Skip to content

Commit

Permalink
Fix deprecated setTo() call in MailBlocker
Browse files Browse the repository at this point in the history
Also includes some minor cleanup of the class.

Refs:
- #37
- wintercms/winter#888 (comment)

Hat-tip to @8blvck and @damanic.
  • Loading branch information
bennothommo committed May 8, 2023
1 parent 4d8e2b4 commit 4c3a154
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions models/MailBlocker.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php namespace Winter\User\Models;
<?php

namespace Winter\User\Models;

use Form;
use Model;
use System\Models\MailTemplate;
use Exception;
use System\Models\MailTemplate;

/**
* Mail Blocker
Expand Down Expand Up @@ -52,7 +53,7 @@ class MailBlocker extends Model
* - verify: Only allow mail templates that are registered in the system.
*
* @param array $templates Template name as key and boolean as value. If false, template is blocked.
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @param array $options
* @return void
*/
Expand All @@ -72,7 +73,7 @@ public static function setPreferences($user, $templates, $options = [])

if ($aliases) {
$fillable = array_merge($fillable, array_values($aliases));
$templates = array_build($templates, function($key, $value) use ($aliases) {
$templates = array_build($templates, function ($key, $value) use ($aliases) {
return [array_get($aliases, $key, $key), $value];
});
}
Expand Down Expand Up @@ -102,7 +103,7 @@ public static function setPreferences($user, $templates, $options = [])
/**
* Adds a block for a user and a mail view/template code.
* @param string $template
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @return bool
*/
public static function addBlock($template, $user)
Expand Down Expand Up @@ -131,7 +132,7 @@ public static function addBlock($template, $user)
/**
* Removes a block for a user and a mail view/template code.
* @param string $template
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @return bool
*/
public static function removeBlock($template, $user)
Expand All @@ -150,7 +151,7 @@ public static function removeBlock($template, $user)
return false;
}

$blocker->each(function($block) {
$blocker->each(function ($block) {
$block->delete();
});

Expand All @@ -159,7 +160,7 @@ public static function removeBlock($template, $user)

/**
* Blocks all mail messages for a user.
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @return bool
*/
public static function blockAll($user)
Expand All @@ -169,7 +170,7 @@ public static function blockAll($user)

/**
* Removes block on all mail messages for a user.
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @return bool
*/
public static function unblockAll($user)
Expand All @@ -179,7 +180,7 @@ public static function unblockAll($user)

/**
* Checks if a user is blocking all templates.
* @param Winter\User\Models\User $user
* @param \Winter\User\Models\User $user
* @return bool
*/
public static function isBlockAll($user)
Expand Down Expand Up @@ -230,19 +231,21 @@ public static function checkForEmail($template, $email)

$emails = array_keys($email);

return static::where(function($q) use ($template) {
$q->where('template', $template)->orWhere('template', '*');
})
->whereIn('email', $emails)
->lists('email');
return static::where(
function ($query) use ($template) {
$query
->where('template', $template)
->orWhere('template', '*');
}
)->whereIn('email', $emails)->lists('email');
}

/**
* Filters a Illuminate\Mail\Message and removes blocked recipients.
* Filters a \Illuminate\Mail\Message and removes blocked recipients.
* If no recipients remain, false is returned. Returns null if mailing
* should proceed.
* @param string $template
* @param Illuminate\Mail\Message $message
* @param \Illuminate\Mail\Message $message
* @return bool|null
*/
public static function filterMessage($template, $message)
Expand All @@ -254,13 +257,13 @@ public static function filterMessage($template, $message)
return null;
}

foreach ($recipients as $address => $name) {
foreach (array_keys($recipients) as $address) {
if (in_array($address, $blockedAddresses)) {
unset($recipients[$address]);
}
}

$message->setTo($recipients);
$message->to($recipients, null, true);
return count($recipients) ? null : false;
}
}

1 comment on commit 4c3a154

@bennothommo
Copy link
Member Author

Choose a reason for hiding this comment

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

Hat-tip to @inerba as well, who had come up with the fix earlier. (#21)

Please sign in to comment.