Skip to content

Check if a user can edit the post/page before sending a notification.#449

Merged
rinatkhaziev merged 7 commits intomasterfrom
fix/265-notification-validation
Dec 19, 2018
Merged

Check if a user can edit the post/page before sending a notification.#449
rinatkhaziev merged 7 commits intomasterfrom
fix/265-notification-validation

Conversation

@WPprodigy
Copy link
Copy Markdown
Contributor

@WPprodigy WPprodigy commented Apr 4, 2018

Part of #265

The functionality is all implemented in the first commit, the other two are cleanup for it's surroundings.

Some things to note:

It's possible that the user_can_be_notified method will need to be abstracted and put into the EF_Module parent class for part 2 of the above issue, but that can be done if/when necessary.

And then it seems calling both edit_post or edit_page conditionally would be redundant according to the core method: https://github.com/WordPress/WordPress/blob/master/wp-includes/capabilities.php#L127-L128. Would be nice to have a second pair of eyes to confirm this, but my testing showed that this was indeed the case. Some more info on what I found here #265 (comment)

Steps for testing

  1. Add multiple authors and editors to a user group.

  2. Using the current master branch, select this user group to be notified on a draft post.

  3. Post an editorial comment.

  4. Look at the email logs or just error_log the $recipients variable at the end of _get_notification_recipients.

  5. Note that all authors/editors that were signed up received this email.

  6. Switch to this branch and post another editorial comment.

  7. Note that only editors were sent the email, along with the post author if they were selected.

Can repeat and test with individual user notifications as well instead or alongside groups.

You shouldn’t receive a notification email unless you are able to view/edit the post.
$authors is never used, so declaring it then merging in an empty array isn’t needed.
General cleanup for the _get_notification_recipients method to align with WP coding standards.

Mostly all spaces, braces, and periods.

The one outlier is the removal of the first declaration of $recipients which is not needed.
Copy link
Copy Markdown
Contributor

@sboisvert sboisvert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions :)

$post_id = $post->ID;
if( !$post_id ) return;

$authors = array();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing these from here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$authors was never used. Was just set to an empty array, and then array_merge'd later on. The missing $post_id conditional is still present, just unfolded per WP coding standards.

Comment thread modules/notifications/notifications.php Outdated

}

$usergroup_users = array();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can bump this up to the top of the function like the others?

Copy link
Copy Markdown
Contributor Author

@WPprodigy WPprodigy May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move $usergroup_users = array(); to the top? I removed all of the top empty variable declarations. One is longer needed, and the others are declared in a more localized place.

Comment thread modules/notifications/notifications.php Outdated
$usergroup_user = get_user_by( 'id', $user_id );
if ( $usergroup_user && is_user_member_of_blog( $user_id ) )
if ( $this->user_can_be_notified( $usergroup_user, $post_id ) ) {
$usergroup_users[] = $usergroup_user->user_email;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between $usergroup_users and $usergroup->user_ids ?
Perhaps we could rename $usergroup_users to something that's clearer, $notified_group_users, $usergroup_to_notify or something along those lines? (This might just be personal preference on my part :) )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the code below, $usergroup_recipients seems like it could work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$usergroup_users is an array of all emails belonging to users that are connected to a group that is following a post. $usergroup->user_ids are the user IDs belonging to a specific user group. There can be multiple user groups connected to a post.

Could definitely use some more specific naming, will make some readability changes.

Comment thread modules/notifications/notifications.php Outdated
// Process the recipients for this email to be sent.
foreach( $recipients as $key => $user_email ) {
// Get rid of empty email entries.
if ( empty( $recipients[ $key ] ) ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When / Why would this happen?

Copy link
Copy Markdown
Contributor Author

@WPprodigy WPprodigy May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good question. This check was legacy code that I left there, but re-reading looks like a possible typo. Should be checking if the email is empty, because it is technically possible to have a user without an email address.

Instead of the conditional, could just wrap array_filter() around the array manipulations above which will simply strip away any empty values when used without a callback function.

WPprodigy added 2 commits May 1, 2018 17:35
When array_filter is used without a callback, it will remove any array items that evalulate to false, namely empty strings and NULLs.
Copy link
Copy Markdown
Contributor

@david-binda david-binda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the code looks good to me, good work on cleaning it up!

I just have flagged some further possible improvements inline. Feel free to hit me with any questions!

Comment thread modules/notifications/notifications.php Outdated
$can_be_notified = $user->has_cap( 'edit_post', $post_id );
}

return apply_filters( 'ef_notification_user_can_be_notified', $can_be_notified, $user, $post_id );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a catch with the $user as it might be boolean false under certain circumstances ( get_user_by call in _get_notification_recipients might return false ). Thus, I would propose to document the filter via PHPDoc notifying the users about that possible issue.

Comment thread modules/notifications/notifications.php Outdated
$can_be_notified = $user->has_cap( 'edit_post', $post_id );
}

return apply_filters( 'ef_notification_user_can_be_notified', $can_be_notified, $user, $post_id );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we cast the filtered value to bool in order to make sure that boolean is always returned from this method? Per PHPDoc comment?

Comment thread modules/notifications/notifications.php Outdated
unset( $recipients[$key] );

$user_recipients = $this->get_following_users( $post_id, 'user_email' );
foreach( (array) $user_recipients as $key => $user ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the typecasting to array really necessary here? Looks like https://github.com/Automattic/Edit-Flow/blob/6fc7cb193c74a48581bdb0c5287ed35c7acbe763/modules/notifications/notifications.php#L956,L1000 is always returning an array.

It might be worth digging into why it has been added to the original code in the first place. Or whether it was in there from the very beginning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be adding it in this PR, so you are right - it's not needed.

@sboisvert
Copy link
Copy Markdown
Contributor

This Looks good. I don't think we want to merge it into master yet but it's ready to go into a feature branch.

@WPprodigy
Copy link
Copy Markdown
Contributor Author

Agreed. I personally think #451 needs to be ready before this goes into a release.

As without that piece, an editor could be mislead into thinking certain people are getting notifications when they are not.

@rinatkhaziev
Copy link
Copy Markdown
Contributor

@sboisvert do we want this to go into 0.8.3 or 0.9?

@rinatkhaziev rinatkhaziev added this to the 0.9 milestone May 3, 2018
@rinatkhaziev
Copy link
Copy Markdown
Contributor

Per Slack - marking this for 0.9

jerclarke added a commit to jerclarke/Edit-Flow that referenced this pull request Nov 8, 2018
user_can_be_notified was already defined in `451-notification-visibility` branch PR Automattic#449
@jerclarke
Copy link
Copy Markdown
Contributor

Tested this and it seems to work, aside from conflicts with the other patches as noted in the referenced links above.

Would love if we could pick an official branch for all these changes and start merging them in. They should all be launched together in 0.9 and they have a lot of interactions that need to be addressed in a merged codebase (since they tend to interact with the same few files).

@rinatkhaziev
Copy link
Copy Markdown
Contributor

@WPprodigy thanks for the work, @jerclarke thanks for the testing. Merging that in to go 0.9 :)

@rinatkhaziev rinatkhaziev merged commit bd49802 into master Dec 19, 2018
@GaryJones GaryJones deleted the fix/265-notification-validation branch December 9, 2019 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants