Skip to content

Instantly share code, notes, and snippets.

@pryley
Created May 4, 2015 22:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pryley/837e7717ba691feab0e1 to your computer and use it in GitHub Desktop.
Save pryley/837e7717ba691feab0e1 to your computer and use it in GitHub Desktop.
This Wordpress plugin adds custom features and modifications to the Ultimate Member plugin family.
<?php
/**
* Plugin Name: Ultimate Member Modifications
* Plugin URI: http://geminilabs.io
* Description: This plugin adds custom features and modifications to the Ultimate Member plugin family.
* Version: 1.0.0
* Author: Gemini Labs
* Author URI: http://geminilabs.io
* License: MIT License
*/
class UltimateMembersMods
{
/**
* Instantiate the class
*
* @return void
*/
public function __construct()
{
// Allows a user to set their Display Name in the Account settings
add_action( 'um_after_account_general', [ $this, 'addDisplayNameFieldToAccount' ] );
// Adds new post notifications to followers
add_filter( 'um_notifications_core_log_types', [ $this, 'notificationsAddTypes' ] );
add_filter( 'um_notifications_get_icon', [ $this, 'notificationsAddIcons' ], 10, 2 );
add_action( 'transition_post_status', [ $this, 'notificationsLogNewPost' ], 10, 3 );
}
/**
* Add the Display Name field to account settings
*
* @return void
*/
public function addDisplayNameFieldToAccount()
{
global $ultimatemember;
$fields = $ultimatemember->builtin->get_specific_fields( 'display_name' );
foreach( $fields as $key => $data ) {
echo $ultimatemember->fields->edit_field( $key, $data );
}
}
/**
* Add custom notification types
*
* @param array $array
*
* @return array
*/
public function notificationsAddTypes( $array )
{
$array['user_post'] = array(
'title' => __( 'New user post', 'geminilabs' ),
'template' => '<strong>{member}</strong> has created a new post: <strong>{post_title}</strong>',
'account_desc' => __( 'When a member you follow publishes a new post', 'geminilabs' ),
);
return $array;
}
/**
* Add custom notification icons
*
* @param string $output
* @param string $type
*
* @return string
*/
public function notificationsAddIcons( $output, $type )
{
if( $type == 'user_post' ) {
$output = '<i class="um-faicon-pencil-square-o" style="color: #9A86FF"></i>';
}
return $output;
}
/**
* Log notifications for new posts
*
* @param string $new_status
* @param string $old_status
* @param WP_Post $post
*
* @return void
*/
public function notificationsLogNewPost( $new_status, $old_status, $post )
{
if( !defined( 'ultimatemember_version' )
|| !defined( 'um_notifications_version' )
|| !defined( 'um_followers_version' )
|| !function_exists( 'um_user' )
|| !function_exists( 'um_fetch_user' )
|| !function_exists( 'um_get_avatar_url' )
|| $new_status == $old_status
|| $new_status != 'publish' ) {
return;
}
global $ultimatemember, $um_followers;
$author = $post->post_author;
$followers = $um_followers->api->followers( $author );
if( is_array( $followers ) ) {
um_fetch_user( $author );
$new = [];
$vars = [];
$vars['member'] = um_user( 'display_name' );
$vars['photo'] = um_get_avatar_url( get_avatar( $author, 40 ) );
$vars['post_title'] = $post->post_title;
$vars['notification_uri'] = get_permalink( $post->ID );
array_walk( $followers, function( $key ) use( &$new ) {
$new[] = $key[ key( $key ) ];
} );
// we only want unique user IDs
$followers = array_keys( array_flip( $new ) );
$this->storeNotifications( $followers, 'user_post', $vars );
}
}
/**
* Store notifications for multiple users
*
* @param array $users
* @param string $type
* @param array $vars
*
* @return void
*/
public function storeNotifications( $users, $type, $vars = [] )
{
global $wpdb, $um_notifications;
$users = $this->usersWithNotificationEnabled( $type, $users );
if( !$users ) {
return;
}
$content = $um_notifications->api->get_notify_content( $type );
if( is_array( $vars ) ) {
foreach( $vars as $key => $var ) {
$content = str_replace( '{'.$key.'}', $var, $content );
}
}
$content = implode( ' ', array_unique( explode( ' ', $content ) ) );
if( $vars && isset( $vars['photo'] ) ) {
$photo = $vars['photo'];
}
else {
if( function_exists( 'um_get_default_avatar_uri' ) ) {
$photo = um_get_default_avatar_uri();
}
else {
// @todo fallback to wordpress method
$photo = '';
}
}
if( $vars && isset( $vars['notification_uri'] ) ) {
$url = $vars['notification_uri'];
}
$table_name = $wpdb->prefix . "um_notifications";
$followers = implode( ',', $users );
$time = current_time( 'mysql' );
// 1. Look for similar logs
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$table_name} WHERE user IN ({$followers}) AND type='%s' AND content='%s' ORDER BY time DESC",
$type,
$content
)
);
// 2. Update any found similar logs
if( !empty( $results ) ) {
array_walk( $results, function( $result ) use( &$notification_updates ) {
$notification_updates[] = $result->user;
} );
$updates = implode( ',', $notification_updates );
$wpdb->check_current_query = false;
$wpdb->query(
$wpdb->prepare(
"UPDATE {$table_name} " .
"SET status = 'unread', time = '%s' " .
"WHERE user IN ({$updates}) AND type='%s' AND content='%s'",
$time,
$type,
$content
)
);
$users = array_diff( $users, $notification_updates );
}
// 3. Insert the rest of the logs
if( !empty( $users ) ) {
foreach( $users as $user_id ) {
$rows[] = "('{$time}',{$user_id},'unread','{$photo}','{$type}','{$url}','{$content}')";
}
$values = implode( ',', $rows );
$wpdb->check_current_query = false;
$wpdb->query(
"INSERT INTO {$table_name} (time,user,status,photo,type,url,content) VALUES{$values}"
);
}
}
/**
* Filter users that have the notification enabled
*
* @param string $key
* @param array $users
*
* @return array|false
*/
public function usersWithNotificationEnabled( $key, $users )
{
if( is_array( $users ) && um_get_option( 'log_'.$key ) ) {
$array = [];
foreach( $users as $i => $user_id ) {
$prefs = get_user_meta( $user_id, '_notifications_prefs', true );
if( $prefs && isset( $prefs[ $key ] ) && !$prefs[ $key ] ) {
unset( $users[ $i ] );
}
}
// re-index array after unset
return array_values( $users );
}
return false;
}
}
new UltimateMembersMods;
@hirenshah
Copy link

Before I break anything, do you know if this still works on the latest versions of UM?

@hirenshah
Copy link

hirenshah commented Dec 15, 2019

Before I break anything, do you know if this still works on the latest versions of UM?

Tried it and had to comment out addDisplayNameFieldToAccount as that broke the account page.

The followers notification fell over with:
PHP Fatal error: Uncaught Error: Call to a member function followers() on null in /home/xxxxx/public_html/sites/xxxxx/wp-content/plugins/um-mods/UltimateMembersMods.php:108

@pryley
Copy link
Author

pryley commented Dec 15, 2019

This gist is over 5 years old. No idea if it works or not.

@hirenshah
Copy link

This gist is over 5 years old. No idea if it works or not.

I managed to fix it :) Other than commenting out the addDisplayNameFieldToAccount part, I had to change these two lines:

		//$followers = $um_followers->api->followers( $author );
		$followers = UM()->Followers_API()->api()->followers( $author );
		//$content = $um_notifications->api->get_notify_content( $type );
		$content = UM()->Notifications_API()->api()->get_notify_content( $type );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment