From 2b5491f94553de22209ef319ab680a733ccf61b0 Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Fri, 4 May 2018 01:28:08 -0500 Subject: [PATCH 1/3] =?UTF-8?q?Add=20a=20=E2=80=9Cno=20access=E2=80=9D=20b?= =?UTF-8?q?adge=20to=20subscribers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WIP --- common/php/class-module.php | 6 +- modules/notifications/lib/notifications.css | 63 +++++++++++++-------- modules/notifications/lib/notifications.js | 24 ++++++-- modules/notifications/notifications.php | 53 +++++++++++++++++ 4 files changed, 116 insertions(+), 30 deletions(-) diff --git a/common/php/class-module.php b/common/php/class-module.php index dff2dd17c..25da3e484 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -472,7 +472,11 @@ function users_select_form( $selected = null, $args = null ) { ID, $selected) ) ? 'checked="checked"' : ''; ?>
  • diff --git a/modules/notifications/lib/notifications.css b/modules/notifications/lib/notifications.css index 85e70084d..493664b13 100644 --- a/modules/notifications/lib/notifications.css +++ b/modules/notifications/lib/notifications.css @@ -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; @@ -91,4 +106,4 @@ #ef-usergroup-users h4 { margin-top: 0; -} \ No newline at end of file +} diff --git a/modules/notifications/lib/notifications.js b/modules/notifications/lib/notifications.js index a5698c882..735d7c39d 100644 --- a/modules/notifications/lib/notifications.js +++ b/modules/notifications/lib/notifications.js @@ -6,6 +6,16 @@ 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.text( '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); @@ -13,9 +23,7 @@ jQuery(document).ready(function($) { 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()); @@ -27,11 +35,17 @@ jQuery(document).ready(function($) { type : 'POST', url : (ajaxurl) ? ajaxurl : wpListL10n.url, data : params, - success : function(x) { + success : function( response ) { 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('

    There was an error. Please reload the page.

    '); diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index 8bc794590..203e8b235 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -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 @@ -354,6 +357,21 @@ function notifications_meta_box() { user_can_be_notified( get_user_by( 'id', $user_id ), $post->ID ) ) { + echo '' . esc_html__( 'No Access', 'edit-flow' ) . ''; + } + } /** * Called when a notification editorial metadata checkbox is checked. Handles saving of a user/usergroup to a post. @@ -381,6 +399,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 ) ); @@ -802,6 +828,33 @@ private function _get_notification_recipients( $post, $string = false ) { } } + /** + * 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 * From ba9dcd798298aedcdab50577082de293926382e8 Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Fri, 4 May 2018 15:28:16 -0500 Subject: [PATCH 2/3] Localize text string --- modules/notifications/lib/notifications.js | 2 +- modules/notifications/notifications.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/notifications/lib/notifications.js b/modules/notifications/lib/notifications.js index 735d7c39d..b556b7a6d 100644 --- a/modules/notifications/lib/notifications.js +++ b/modules/notifications/lib/notifications.js @@ -11,7 +11,7 @@ jQuery(document).ready(function($) { $( container ).siblings( 'span' ).remove(); } else if ( user_has_no_access ) { var span = $( '' ); - span.text( 'No Access' ); + span.text( ef_notifications_localization.no_access ); $( container ).parent().prepend( span ); } } diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index 203e8b235..cd947bb2f 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -194,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' ) + ) + ); } } From 6f4b5e80c0495912e9262de6769faf5a3c97c752 Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Fri, 4 May 2018 15:51:26 -0500 Subject: [PATCH 3/3] Spacing / reduce indentation --- common/php/class-module.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/php/class-module.php b/common/php/class-module.php index 25da3e484..8c6ac5527 100755 --- a/common/php/class-module.php +++ b/common/php/class-module.php @@ -473,8 +473,8 @@ function users_select_form( $selected = null, $args = null ) {