Skip to content

Commit d62781e

Browse files
mikeyarcecojennin
authored andcommitted
Add scripts for notification meta box
Add inline JS variables `post_author_id` - the ID of the post author `post_author_is_follower` - if the post_author is subscribed to post notifications `post_author_auto_subscribe` - if post authors are set to be automatically subscribed
1 parent ef25a88 commit d62781e

File tree

6 files changed

+249
-98
lines changed

6 files changed

+249
-98
lines changed

common/php/class-module.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,20 @@ function users_select_form( $selected = null, $args = null ) {
495495
// Add a class to checkbox of current user so we know not to add them in notified list during notifiedMessage() js function
496496
$current_user_class = ( get_current_user_id() == $user->ID ) ? 'class="post_following_list-current_user" ' : '';
497497
?>
498-
<li>
498+
<li class="ef-user-list-item">
499499
<label for="<?php echo esc_attr( $input_id .'-'. $user->ID ) ?>">
500-
<div class="ef-user-subscribe-actions">
500+
<div class="ef-user-subscribe-action">
501501
<?php do_action( 'ef_user_subscribe_actions', $user->ID, $checked ) ?>
502502
<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; echo $current_user_class; ?> />
503503
</div>
504504

505-
<span class="ef-user_displayname"><?php echo esc_html( $user->display_name ); ?></span>
506-
<span class="ef-user_useremail"><?php echo esc_html( $user->user_email ); ?></span>
505+
<div class="ef-user-list_info">
506+
<span class="ef-user_displayname"><?php echo esc_html( $user->display_name ); ?></span>
507+
<span class="ef-user_useremail"><?php echo esc_html( $user->user_email ); ?></span>
508+
<div class="ef-user-list-badges">
509+
<?php do_action( 'ef_user_subscribe_badges', $user->ID, $checked ) ?>
510+
</div>
511+
</div>
507512
</label>
508513
</li>
509514
<?php endforeach; ?>

modules/notifications/lib/notifications.css

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
}
88

99
.ef-post_following_list li {
10+
border-bottom: 1px solid #ccc;
11+
}
12+
13+
.ef-post_following_list li label {
14+
min-height: 76px;
1015
padding: 10px 5px 5px 5px;
1116
margin: 0;
12-
border-bottom: 1px solid #ccc;
13-
min-height: 36px;
17+
display: block;
1418
}
1519

16-
.ef-post_following_list li:hover {
20+
.ef-post_following_list li label:hover {
1721
background: #EAF2FA;
1822
}
1923

@@ -40,12 +44,21 @@
4044
margin-top: 5px;
4145
}
4246

43-
.ef-post_following_list li .ef-user-subscribe-actions .post_following_list-no_email,
44-
.ef-post_following_list li .ef-user-subscribe-actions .post_following_list-no_access {
47+
.ef-post_following_list li .ef-user-list-badges {
48+
margin-top: 10px;
49+
}
50+
51+
.ef-post_following_list li .ef-user-badge {
4552
margin-right: 10px;
46-
border: solid 2px #DC3232;
47-
color: #DC3232;
53+
border: solid 2px #b8b8b8;
54+
color: #b8b8b8;
4855
padding: 4px;
56+
display: inline-block;
57+
}
58+
59+
.ef-post_following_list li .ef-user-badge-error {
60+
color: #DC3232;
61+
border: solid 2px #DC3232;
4962
}
5063

5164
#ef-post_following_box {
@@ -107,4 +120,4 @@
107120

108121
#ef-usergroup-users h4 {
109122
margin-top: 0;
110-
}
123+
}
Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,136 @@
1+
const { subscribe, select } = wp.data;
2+
3+
const BADGES_STATUS = {
4+
error: 'error',
5+
warning: 'warning',
6+
success: 'success'
7+
}
8+
9+
const BADGES = {
10+
NO_ACCESS: {
11+
id: 'no_access',
12+
name: ef_notifications_localization.no_access,
13+
status: BADGES_STATUS['error']
14+
},
15+
NO_EMAIL: {
16+
id: 'no_email',
17+
name: ef_notifications_localization.no_email,
18+
status: BADGES_STATUS['error']
19+
},
20+
POST_AUTHOR: {
21+
id: 'post_author',
22+
name: ef_notifications_localization.post_author,
23+
class: 'ef-badge-neutral'
24+
},
25+
AUTO_SUBSCRIBE: {
26+
id: 'auto_subscribed',
27+
name: ef_notifications_localization.auto_subscribed,
28+
class: 'ef-badge-neutral'
29+
},
30+
}
31+
32+
const addBadgeToEl = ( $el, badge ) => {
33+
if ( getBadge( $el, badge ) ) {
34+
return;
35+
}
36+
37+
$el.append( badgeTemplate(badge ) )
38+
}
39+
40+
const removeBadgeFromEl = ( $el, badge ) => {
41+
const existingBadge = getBadge( $el, badge );
42+
43+
if ( !existingBadge ) {
44+
return;
45+
}
46+
47+
existingBadge.remove();
48+
}
49+
50+
const getBadge = ( $el, badge ) => {
51+
const exists = $el.find( `[data-badge-id='${badge.id}']` );
52+
53+
if ( exists.length ) {
54+
return jQuery( exists[0] );
55+
} else {
56+
return null;
57+
}
58+
}
59+
60+
const badgeTemplate = ( badge ) => {
61+
let classes = 'ef-user-badge';
62+
63+
if ( BADGES_STATUS['error'] === badge.status ) {
64+
classes += ' ef-user-badge-error';
65+
}
66+
67+
return `<div class="${classes}" data-badge-id="${badge.id}">${badge.name}</div>`
68+
}
69+
170
jQuery(document).ready(function($) {
2-
$('#ef-post_following_users_box ul').listFilterizer();
71+
jQuery('#ef-post_following_users_box ul').listFilterizer();
372

473
var params = {
574
action: 'save_notifications',
6-
post_id: $('#post_ID').val(),
75+
post_id: jQuery('#post_ID').val(),
776
};
77+
878

979
var toggle_warning_badges = function( container, response ) {
10-
// Remove any existing badges
11-
if ( $( container ).siblings( 'span' ).length ) {
12-
$( container ).siblings( 'span' ).remove();
13-
}
14-
80+
const $el = jQuery( container ).parent();
81+
const $badgesContainer = $el.closest( 'li' ).find( '.ef-user-list-badges' );
1582
// "No Access" If this user was flagged as not having access
16-
var user_has_no_access = response.data.subscribers_with_no_access.includes( parseInt( $( container ).val() ) );
83+
var user_has_no_access = response.data.subscribers_with_no_access.includes( parseInt( jQuery( container ).val() ) );
1784
if ( user_has_no_access ) {
18-
var span = $( '<span />' ).addClass( 'post_following_list-no_access' );
19-
span.text( ef_notifications_localization.no_access );
20-
$( container ).parent().prepend( span );
21-
warning_background = true;
85+
addBadgeToEl( $badgesContainer, BADGES[ 'NO_ACCESS' ] );
86+
} else {
87+
removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_ACCESS' ] );
2288
}
89+
2390
// "No Email" If this user was flagged as not having an email
24-
var user_has_no_email = response.data.subscribers_with_no_email.includes( parseInt( $( container ).val() ) );
91+
var user_has_no_email = response.data.subscribers_with_no_email.includes( parseInt( jQuery( container ).val() ) );
2592
if ( user_has_no_email ) {
26-
var span = $( '<span />' ).addClass( 'post_following_list-no_email' );
27-
span.text( ef_notifications_localization.no_email );
28-
$( container ).parent().prepend( span );
29-
warning_background = true;
93+
addBadgeToEl( $badgesContainer, BADGES[ 'NO_EMAIL' ] );
94+
} else {
95+
removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_EMAIL' ] );
3096
}
3197
}
3298

33-
$(document).on('click','.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox', function() {
99+
const show_post_author_badge = () => {
100+
const $userListItemActions = jQuery("label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges");
101+
addBadgeToEl( $userListItemActions, BADGES[ 'POST_AUTHOR' ] );
102+
}
103+
104+
show_post_author_badge();
105+
106+
107+
const show_autosubscribed_badge = () => {
108+
const $userListItemActions = jQuery("label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges");
109+
addBadgeToEl( $userListItemActions, BADGES[ 'AUTO_SUBSCRIBE' ] );
110+
}
111+
112+
const disable_autosubscribe_checkbox = () => {
113+
jQuery('#ef-selected-users-' + ef_post_author_id ).prop( 'disabled', true );
114+
}
115+
116+
if ( typeof ef_post_author_auto_subscribe !== 'undefined' ) {
117+
show_autosubscribed_badge();
118+
disable_autosubscribe_checkbox();
119+
}
120+
121+
122+
123+
jQuery(document).on('click','.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox', function() {
34124
var user_group_ids = [];
35-
var parent_this = $(this);
36-
params.ef_notifications_name = $(this).attr('name');
37-
params._nonce = $("#ef_notifications_nonce").val();
125+
var parent_this = jQuery(this);
126+
params.ef_notifications_name = jQuery(this).attr('name');
127+
params._nonce = jQuery("#ef_notifications_nonce").val();
38128

39-
$(this)
129+
jQuery(this)
40130
.parents('.ef-post_following_list')
41131
.find('input:checked')
42132
.map(function(){
43-
user_group_ids.push($(this).val());
133+
user_group_ids.push(jQuery(this).val());
44134
})
45135

46136
params.user_group_ids = user_group_ids;
@@ -56,25 +146,25 @@ jQuery(document).ready(function($) {
56146

57147
// Toggle the warning badges ("No Access" and "No Email") to signal the user won't receive notifications
58148
if ( undefined !== response.data ) {
59-
toggle_warning_badges( $( parent_this ), response );
149+
toggle_warning_badges( jQuery( parent_this ), response );
60150
}
61151
// Green 40% by default
62152
var backgroundHighlightColor = "#90d296";
63153
if ( warning_background ) {
64154
// Red 40% if there's a warning
65155
var backgroundHighlightColor = "#ea8484";
66156
}
67-
var backgroundColor = parent_this.css( 'background-color' );
68-
$(parent_this.parents('li'))
157+
var backgroundColor = 'transparent';
158+
jQuery(parent_this.parents('label'))
69159
.animate( { 'backgroundColor': backgroundHighlightColor }, 200 )
70-
.animate( { 'backgroundColor':backgroundColor }, 200 );
160+
.animate( { 'backgroundColor': backgroundColor }, 200 );
71161

72162
// This event is used to show an updated list of who will be notified of editorial comments and status updates.
73-
$( '#ef-post_following_box' ).trigger( 'following_list_updated' );
163+
jQuery( '#ef-post_following_box' ).trigger( 'following_list_updated' );
74164
},
75165
error : function(r) {
76-
$('#ef-post_following_users_box').prev().append(' <p class="error">There was an error. Please reload the page.</p>');
166+
jQuery('#ef-post_following_users_box').prev().append(' <p class="error">There was an error. Please reload the page.</p>');
77167
}
78168
});
79169
});
80-
});
170+
});

modules/notifications/notifications.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function init() {
7171
add_action( 'add_meta_boxes', array( $this, 'add_post_meta_box' ) );
7272

7373
// Add "access badge" to the subscribers list.
74-
add_action( 'ef_user_subscribe_actions', array( $this, 'display_subscriber_warning_badges' ), 10, 2 );
74+
add_action( 'ef_user_subscribe_badges', array( $this, 'display_subscriber_warning_badges' ), 10, 2 );
7575

7676
// Saving post actions
7777
// self::save_post_subscriptions() is hooked into transition_post_status so we can ensure usergroup data
@@ -193,13 +193,15 @@ function enqueue_admin_scripts() {
193193
if ( $this->is_whitelisted_functional_view() ) {
194194
wp_enqueue_script( 'jquery-listfilterizer' );
195195
wp_enqueue_script( 'jquery-quicksearch' );
196-
wp_enqueue_script( 'edit-flow-notifications-js', $this->module_url . 'lib/notifications.js', array( 'jquery', 'jquery-listfilterizer', 'jquery-quicksearch' ), EDIT_FLOW_VERSION, true );
196+
wp_enqueue_script( 'edit-flow-notifications-js', EDIT_FLOW_URL . 'modules/dist/notifications.build.js', array( 'jquery', 'jquery-listfilterizer', 'jquery-quicksearch' ), EDIT_FLOW_VERSION, true );
197197
wp_localize_script(
198198
'edit-flow-notifications-js',
199199
'ef_notifications_localization',
200200
array(
201201
'no_access' => esc_html__( 'No Access', 'edit-flow' ),
202-
'no_email' => esc_html__( 'No Email', 'edit-flow' )
202+
'no_email' => esc_html__( 'No Email', 'edit-flow' ),
203+
'post_author' => esc_html__( 'Post Author', 'edit-flow' ),
204+
'auto_subscribed' => esc_html__( 'Auto-Subscribed', 'edit-flow' )
203205
)
204206
);
205207
}
@@ -343,6 +345,17 @@ function notifications_meta_box() {
343345
<h4><?php _e( 'Users', 'edit-flow' ); ?></h4>
344346
<?php
345347
$followers = $this->get_following_users( $post->ID, 'id' );
348+
349+
$post_author_is_follower = ! empty( in_array( $post->post_author, $followers ) ) ? 'true' : 'false' ;
350+
$post_author_auto_subscribe = apply_filters( 'ef_notification_auto_subscribe_post_author', true, 'subscription_action' ) ? 'true' : 'false' ;
351+
352+
wp_add_inline_script( 'edit-flow-notifications-js',
353+
'var ef_post_author_id = ' . wp_json_encode( $post->post_author ) . '; ' .
354+
'var ef_post_author_is_follower = ' . $post_author_is_follower . '; ' .
355+
'var ef_post_author_auto_subscribe = ' . $post_author_auto_subscribe . ';',
356+
'before'
357+
);
358+
346359
$select_form_args = array(
347360
'list_class' => 'ef-post_following_list',
348361
);
@@ -384,14 +397,14 @@ function display_subscriber_warning_badges( $user_id, $checked ) {
384397
// Add No Access span if they won't be notified
385398
if (! $this->user_can_be_notified( get_user_by( 'id', $user_id ), $post->ID )) {
386399
// span.post_following_list-no_access is also added in notifications.js after AJAX that ticks/unticks a user
387-
echo '<span class="post_following_list-no_access">' . esc_html__( 'No Access', 'edit-flow' ) . '</span>';
400+
echo '<div class="ef-user-badge ef-user-badge-error" data-badge-id="no_access">' . esc_html__( 'No Access', 'edit-flow' ) . '</div>';
388401
}
389402

390403
// Add No Email span if they have no email
391404
$user_object = get_user_by( 'id', $user_id );
392405
if ( !is_a( $user_object, 'WP_User') OR empty( $user_object->user_email ) ) {
393406
// span.post_following_list-no_email is also added in notifications.js after AJAX that ticks/unticks a user
394-
echo '<span class="post_following_list-no_email">' . esc_html__( 'No Email', 'edit-flow' ) . '</span>';
407+
echo '<div class="ef-user-badge ef-user-badge-error" data-badge-id="no_email">' . esc_html__( 'No Email', 'edit-flow' ) . '</div>';
395408
}
396409
}
397410

modules/user-groups/lib/user-groups.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
}
44

55
.ef-post_following_list li {
6-
padding: 10px 5px 5px 5px;
76
margin: 0;
87
border-bottom: 1px solid #ccc;
98
}
10-
.ef-post_following_list li:hover {
11-
background: #EAF2FA;
12-
}
9+
10+
.ef-post_following_list li:hover {
11+
background: #EAF2FA;
12+
}
13+
1314
.ef-post_following_list li input {
1415
float: right;
1516
}

0 commit comments

Comments
 (0)