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

Fix wp_mail still working for temporary users #6868

Merged
merged 6 commits into from
May 15, 2023
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
4 changes: 4 additions & 0 deletions changelog/fix-wpmail-temporary-users
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Block triggering wp_mail for temporary users even on non-frontend context.
9 changes: 4 additions & 5 deletions includes/class-sensei-guest-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
add_action( 'sensei_can_access_course_content', [ $this, 'open_course_enable_course_access' ], 10, 2 );
add_action( 'sensei_can_user_manually_enrol', [ $this, 'open_course_user_can_manualy_enroll' ], 10, 2 );
add_filter( 'sensei_send_emails', [ $this, 'skip_sensei_email' ] );
add_filter( 'pre_wp_mail', [ $this, 'skip_wp_mail' ], 10, 2 );

$this->create_guest_student_role_if_not_exists();

Expand Down Expand Up @@ -307,7 +306,7 @@
* @since 4.11.0
* @access private
*/
private function is_current_user_guest() {
private static function is_current_user_guest() {
$user = wp_get_current_user();
return self::is_guest_user( $user );
}
Expand Down Expand Up @@ -475,7 +474,7 @@
* @return boolean Whether to send the email.
*/
public function skip_sensei_email( $send_email ) {
return $this->is_current_user_guest() ? false : $send_email;
return self::is_current_user_guest() ? false : $send_email;

Check warning on line 477 in includes/class-sensei-guest-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-guest-user.php#L477

Added line #L477 was not covered by tests
}

/**
Expand All @@ -488,10 +487,10 @@
* @param array $atts Email attributes.
* @return bool|null Null if we should send the email, a boolean if not.
*/
public function skip_wp_mail( $return, $atts ) {
if ( $this->is_current_user_guest() ) {
public static function skip_wp_mail( $return, $atts ) {
if ( self::is_current_user_guest() ) {
// If this e-mail is being dispatched while the current user is a guest, just... don't send it.
return false;

Check warning on line 493 in includes/class-sensei-guest-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-guest-user.php#L493

Added line #L493 was not covered by tests
}
if ( Sensei_Temporary_User::should_block_email( $atts, self::EMAIL_DOMAIN ) ) {
// If this e-mail is being dispatched to a guest user, don't send it.
Expand Down
15 changes: 7 additions & 8 deletions includes/class-sensei-preview-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
add_action( 'show_admin_bar', [ $this, 'show_admin_bar_to_preview_user' ], 90 );
add_action( 'admin_bar_menu', [ $this, 'add_user_switch_to_admin_bar' ], 90 );
add_filter( 'sensei_is_enrolled', [ $this, 'preview_user_always_enrolled' ], 90, 3 );
add_filter( 'pre_wp_mail', [ $this, 'skip_wp_mail' ], 10, 2 );

$this->create_role();
}
Expand All @@ -103,7 +102,7 @@
* @access private
*/
public function add_preview_user_filters() {
if ( $this->is_preview_user_active() ) {
if ( self::is_preview_user_active() ) {
add_filter( 'map_meta_cap', [ $this, 'allow_post_preview' ], 10, 4 );
add_filter( 'pre_get_posts', [ $this, 'count_unpublished_lessons' ], 10 );
add_filter( 'sensei_notice', [ $this, 'hide_notices' ], 10, 1 );
Expand Down Expand Up @@ -195,7 +194,7 @@
return;
}

if ( $this->can_switch_to_preview_user( $course_id ) && ! $this->is_preview_user_active() ) {
if ( $this->can_switch_to_preview_user( $course_id ) && ! self::is_preview_user_active() ) {

Check warning on line 197 in includes/class-sensei-preview-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-preview-user.php#L197

Added line #L197 was not covered by tests
$wp_admin_bar->add_node(
[
'id' => self::SWITCH_ON_ACTION,
Expand All @@ -209,7 +208,7 @@
);
}

if ( $this->is_preview_user_active() ) {
if ( self::is_preview_user_active() ) {

Check warning on line 211 in includes/class-sensei-preview-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-preview-user.php#L211

Added line #L211 was not covered by tests
$wp_admin_bar->add_node(
[
'id' => self::SWITCH_OFF_ACTION,
Expand All @@ -236,7 +235,7 @@
* @return bool
*/
public function show_admin_bar_to_preview_user( $show ) {
if ( $this->is_preview_user_active() ) {
if ( self::is_preview_user_active() ) {

Check warning on line 238 in includes/class-sensei-preview-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-preview-user.php#L238

Added line #L238 was not covered by tests
return true;
}

Expand Down Expand Up @@ -264,10 +263,10 @@
* @param array $atts Email attributes.
* @return bool|null Null if we should send the email, a boolean if not.
*/
public function skip_wp_mail( $return, $atts ) {
if ( $this->is_preview_user_active() ) {
public static function skip_wp_mail( $return, $atts ) {
if ( self::is_preview_user_active() ) {
// If this e-mail is being dispatched while the current user is a previwe user, just... don't send it.
return false;

Check warning on line 269 in includes/class-sensei-preview-user.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-preview-user.php#L269

Added line #L269 was not covered by tests
}
if ( Sensei_Temporary_User::should_block_email( $atts, self::EMAIL_DOMAIN ) ) {
// If this e-mail is being dispatched to a preview user, don't send it.
Expand Down Expand Up @@ -481,7 +480,7 @@
*
* @return bool
*/
private function is_preview_user_active() {
private static function is_preview_user_active() {
$user = wp_get_current_user();
return self::is_preview_user( $user );
}
Expand Down
2 changes: 2 additions & 0 deletions includes/class-sensei-temporary-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static function init() {
add_filter( 'sensei_learners_query', [ static::class, 'filter_learners_query' ] );
add_filter( 'sensei_count_statuses_args', [ static::class, 'filter_count_statuses' ] );
add_filter( 'sensei_check_for_activity', [ static::class, 'filter_sensei_activity' ], 10, 2 );
add_filter( 'pre_wp_mail', [ Sensei_Guest_User::class, 'skip_wp_mail' ], 10, 2 );
add_filter( 'pre_wp_mail', [ Sensei_Preview_User::class, 'skip_wp_mail' ], 10, 2 );
Comment on lines +38 to +39
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a huge fan of this change: basically, I'm adding the filters here for each class.

I thought about maybe creating a method in each class just to run that add_filter and call it here, but I think the result would be...quite similar, so that's why I left it this way.

This is also the main change that fixes the issue, so..yeah, there's that.

}

/**
Expand Down
Loading