Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "No Access" badge to subscribers that will not be able to receive notifications #461

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion common/php/class-module.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,11 @@ function users_select_form( $selected = null, $args = null ) {
<?php $checked = ( in_array($user->ID, $selected) ) ? 'checked="checked"' : ''; ?>
<li>
<label for="<?php echo esc_attr( $input_id .'-'. $user->ID ) ?>">
<input type="checkbox" id="<?php echo esc_attr( $input_id .'-'. $user->ID ) ?>" name="<?php echo esc_attr( $input_id ) ?>[]" value="<?php echo esc_attr( $user->ID ); ?>" <?php echo $checked; ?> />
<div class="ef-user-subscribe-actions">
<?php do_action( 'ef_user_subscribe_actions', $user->ID, $checked ) ?>
<input type="checkbox" id="<?php echo esc_attr( $input_id .'-'. $user->ID ) ?>" name="<?php echo esc_attr( $input_id ) ?>[]" value="<?php echo esc_attr( $user->ID ); ?>" <?php echo $checked; ?> />
</div>

<span class="ef-user_displayname"><?php echo esc_html( $user->display_name ); ?></span>
<span class="ef-user_useremail"><?php echo esc_html( $user->user_email ); ?></span>
</label>
Expand Down
63 changes: 39 additions & 24 deletions modules/notifications/lib/notifications.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,46 @@
background-position: 5px 7px;
}

.ef-post_following_list {
.ef-post_following_list li {
padding: 10px 5px 5px 5px;
margin: 0;
border-bottom: 1px solid #ccc;
min-height: 36px;
}

.ef-post_following_list li {
padding: 10px 5px 5px 5px;
margin: 0;
border-bottom: 1px solid #ccc;
}
.ef-post_following_list li:hover {
background: #EAF2FA;
}
.ef-post_following_list li input {
float: right;
}
.ef-post_following_list .ef-user_displayname,
.ef-post_following_list .ef-usergroup_name {
display: block;
font-size: 14px;
}
.ef-post_following_list .ef-user_useremail,
.ef-post_following_list .ef-usergroup_description {
display: block;
color: #ccc;
font-size: 12px;
}
.ef-post_following_list li:hover {
background: #EAF2FA;
}

.ef-post_following_list li input {
float: right;
margin-top: 0;
}

.ef-post_following_list .ef-user_displayname,
.ef-post_following_list .ef-usergroup_name {
display: block;
font-size: 14px;
}

.ef-post_following_list .ef-user_useremail,
.ef-post_following_list .ef-usergroup_description {
display: block;
color: #ccc;
font-size: 12px;
}

.ef-post_following_list li .ef-user-subscribe-actions {
float: right;
margin-top: 5px;
}

.ef-post_following_list li .ef-user-subscribe-actions span {
margin-right: 10px;
border: solid 2px #FFCCCC;
color: #FFCCCC;
padding: 4px;
}

#ef-post_following_box {
margin:10px 0;
Expand Down Expand Up @@ -91,4 +106,4 @@

#ef-usergroup-users h4 {
margin-top: 0;
}
}
25 changes: 20 additions & 5 deletions modules/notifications/lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ jQuery(document).ready(function($) {
post_id: $('#post_ID').val(),
};

var toggle_no_access_badge = function( container, user_has_no_access ) {
if ( $( container ).siblings( 'span' ).length ) {
$( container ).siblings( 'span' ).remove();
} else if ( user_has_no_access ) {
var span = $( '<span />' );
span.text( ef_notifications_localization.no_access );
$( container ).parent().prepend( span );
}
}

$(document).on('click','.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox', function() {
var user_group_ids = [];
var parent_this = $(this);
params.ef_notifications_name = $(this).attr('name');
params._nonce = $("#ef_notifications_nonce").val();

$(this)
.parent()
.parent()
.parent()
.parents('.ef-post_following_list')
.find('input:checked')
.map(function(){
user_group_ids.push($(this).val());
Expand All @@ -27,15 +35,22 @@ jQuery(document).ready(function($) {
type : 'POST',
url : (ajaxurl) ? ajaxurl : wpListL10n.url,
data : params,
success : function(x) {

success : function( response ) {

// This event is used to show an updated list of who will be notified of editorial comments and status updates.
$( '#ef-post_following_box' ).trigger( 'following_list_updated' );

var backgroundColor = parent_this.css( 'background-color' );
$(parent_this.parent().parent())
$(parent_this.parents('li'))
.animate( { 'backgroundColor':'#CCEEBB' }, 200 )
.animate( { 'backgroundColor':backgroundColor }, 200 );

// Toggle the "No Access" badge if the selected user does not have access.
if ( undefined !== response.data ) {
var user_has_no_access = response.data.subscribers_with_no_access.includes( parseInt( $( parent_this ).val() ) );
toggle_no_access_badge( $( parent_this ), user_has_no_access );
}
},
error : function(r) {
$('#ef-post_following_users_box').prev().append(' <p class="error">There was an error. Please reload the page.</p>');
Expand Down
60 changes: 60 additions & 0 deletions modules/notifications/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function init() {

// Set up metabox and related actions
add_action( 'add_meta_boxes', array( $this, 'add_post_meta_box' ) );

// Add "access badge" to the subscribers list.
add_action( 'ef_user_subscribe_actions', array( $this, 'display_subscriber_access_badge' ), 10, 2 );

// Saving post actions
// self::save_post_subscriptions() is hooked into transition_post_status so we can ensure usergroup data
Expand Down Expand Up @@ -191,6 +194,13 @@ function enqueue_admin_scripts() {
wp_enqueue_script( 'jquery-listfilterizer' );
wp_enqueue_script( 'jquery-quicksearch' );
wp_enqueue_script( 'edit-flow-notifications-js', $this->module_url . 'lib/notifications.js', array( 'jquery', 'jquery-listfilterizer', 'jquery-quicksearch' ), EDIT_FLOW_VERSION, true );
wp_localize_script(
'edit-flow-notifications-js',
'ef_notifications_localization',
array(
'no_access' => esc_html__( 'No Access', 'edit-flow' )
)
);
}
}

Expand Down Expand Up @@ -354,6 +364,21 @@ function notifications_meta_box() {

<?php
}

/**
* Show a badge next to a subscriber's name showing if they have the permissions needed to recieve notifications.
*
* @param int $user_id
* @param bool $checked True if the user is subscribed already, false otherwise.
* @return void
*/
function display_subscriber_access_badge( $user_id, $checked ) {
global $post;

if ( isset( $post ) && $checked && ! $this->user_can_be_notified( get_user_by( 'id', $user_id ), $post->ID ) ) {
echo '<span>' . esc_html__( 'No Access', 'edit-flow' ) . '</span>';
}
}

/**
* Called when a notification editorial metadata checkbox is checked. Handles saving of a user/usergroup to a post.
Expand Down Expand Up @@ -381,6 +406,14 @@ function ajax_save_post_subscriptions() {

if ( 'ef-selected-users[]' === $_POST['ef_notifications_name'] ) {
$this->save_post_following_users( $post, $user_group_ids );

if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['post_id'] ) ) {
$subscribers_with_no_access = array_filter( $user_group_ids, function( $user_id ) {
return ! $this->user_can_be_notified( get_user_by( 'id', $user_id ), $_POST['post_id'] );
} );

wp_send_json_success( array( 'subscribers_with_no_access' => array_values( $subscribers_with_no_access ) ) );
}
}

$groups_enabled = $this->module_enabled( 'user_groups' ) && in_array( get_post_type( $post_id ), $this->get_post_types_for_module( $edit_flow->user_groups->module ) );
Expand Down Expand Up @@ -838,6 +871,33 @@ function user_can_be_notified( $user, $post_id ) {
return (bool) apply_filters( 'ef_notification_user_can_be_notified', $can_be_notified, $user, $post_id );
}

/**
* TODO: Remove this before merge. Duplicated function originally in PR #449
*
* Check if a user can be notified.
* This is based off of the ability to edit the post/page by default.
*
* @since 0.8.3
* @param WP_User $user
* @param int $post_id
* @return bool True if the user can be notified, false otherwise.
*/
function user_can_be_notified( $user, $post_id ) {
$can_be_notified = false;
if ( $user instanceof WP_User && is_user_member_of_blog( $user->ID ) && is_numeric( $post_id ) ) {
// The 'edit_post' cap check also covers the undocumented 'edit_page' cap.
$can_be_notified = $user->has_cap( 'edit_post', $post_id );
}
/**
* Filters if a user can be notified. Defaults to true if they can edit the post/page.
*
* @param bool $can_be_notified True if the user can be notified.
* @param WP_User|bool $user The user object, otherwise false.
* @param int $post_id The post the user will be notified about.
*/
return (bool) apply_filters( 'ef_notification_user_can_be_notified', $can_be_notified, $user, $post_id );
}

/**
* Set a user or users to follow a post
*
Expand Down