| @@ -58,15 +58,12 @@ | ||
| 58 | 58 | return []; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | - * Returns the mailboxes the current agent is allowed to know about. | |
| 62 | + * Mailboxes the current agent isn't restricted from. The restriction list is | |
| 63 | + * the only gate, so an unrestricted agent gets `settings` (incl. | |
| 64 | + * admin_email_address) for every box. | |
| 63 | 65 | * |
| 64 | - * Restricted inboxes are filtered out, so the `settings` blob only ever describes | |
| 65 | - * an inbox the agent already works in. Note that the restriction list is the only | |
| 66 | - * gate here: an agent with no restrictions receives `settings` — including | |
| 67 | - * `admin_email_address` — for every inbox, regardless of their other permissions. | |
| 68 | - * | |
| 69 | 66 | * @param bool $withEmail Include the inbox address. Only pass true once the caller |
| 70 | 67 | * has confirmed the current user holds `fst_sensitive_data`. |
| 71 | 68 | * @return \FluentSupport\Framework\Database\Orm\Collection |
| 72 | 69 | */ |
| @@ -71,9 +68,9 @@ | ||
| 71 | 68 | * @return \FluentSupport\Framework\Database\Orm\Collection |
| 72 | 69 | */ |
| 73 | 70 | public static function getAccessibleBoxes($withEmail = false) |
| 74 | 71 | { |
| 75 | - $columns = ['id', 'name', 'settings']; | |
| 72 | + $columns = ['id', 'name', 'box_type', 'settings']; | |
| 76 | 73 | |
| 77 | 74 | if ($withEmail) { |
| 78 | 75 | $columns[] = 'email'; |
| 79 | 76 | } |
| @@ -97,14 +94,25 @@ | ||
| 97 | 94 | } |
| 98 | 95 | return $slug; |
| 99 | 96 | } |
| 100 | 97 | |
| 101 | - public function getMailerHeader() | |
| 98 | + /** From-name choices for email an agent writes (settings.from_name_type). */ | |
| 99 | + const FROM_NAME_MAILBOX = 'mailbox'; | |
| 100 | + const FROM_NAME_AGENT = 'agent'; | |
| 101 | + const FROM_NAME_CUSTOM = 'custom'; | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @param \FluentSupport\App\Models\Agent|null $agent The agent who wrote the email. | |
| 105 | + * Only agent-written email passes one; automatic email keeps the inbox name. | |
| 106 | + * @return array | |
| 107 | + */ | |
| 108 | + public function getMailerHeader($agent = null) | |
| 102 | 109 | { |
| 103 | 110 | $headers = []; |
| 104 | - $fromString = $this->email; //$this->name; | |
| 105 | - if($this->name) { | |
| 106 | - $fromString = $this->name.' <'.$fromString.'>'; | |
| 111 | + $fromString = $this->email; | |
| 112 | + $fromName = $this->getFromName($agent); | |
| 113 | + if ($fromName) { | |
| 114 | + $fromString = $fromName . ' <' . $fromString . '>'; | |
| 107 | 115 | } |
| 108 | 116 | |
| 109 | 117 | if ($fromString) { |
| 110 | 118 | $headers[] = 'From: '. $fromString; |
| @@ -109,15 +117,59 @@ | ||
| 109 | 117 | if ($fromString) { |
| 110 | 118 | $headers[] = 'From: '. $fromString; |
| 111 | 119 | } |
| 112 | 120 | |
| 113 | - // Set Reply-To Header | |
| 114 | 121 | $headers[] = 'Reply-To: '. $fromString; |
| 115 | - $headers[] = 'In-Reply-To: '. $fromString; | |
| 116 | 122 | |
| 117 | 123 | return $headers; |
| 118 | 124 | } |
| 119 | 125 | |
| 126 | + /** | |
| 127 | + * Display name for the From header. The address is always the inbox email, so | |
| 128 | + * replies thread back; only the name follows the inbox's From name setting. | |
| 129 | + * | |
| 130 | + * @param \FluentSupport\App\Models\Agent|null $agent | |
| 131 | + * @return string | |
| 132 | + */ | |
| 133 | + public function getFromName($agent = null) | |
| 134 | + { | |
| 135 | + $settings = is_array($this->settings) ? $this->settings : []; | |
| 136 | + $type = isset($settings['from_name_type']) ? $settings['from_name_type'] : self::FROM_NAME_MAILBOX; | |
| 137 | + $name = ''; | |
| 138 | + | |
| 139 | + if ($agent && $type === self::FROM_NAME_AGENT) { | |
| 140 | + $name = $agent->full_name; | |
| 141 | + } elseif ($agent && $type === self::FROM_NAME_CUSTOM && !empty($settings['from_name_template'])) { | |
| 142 | + $name = apply_filters('fluent_support/parse_smartcode_data', $settings['from_name_template'], [ | |
| 143 | + 'agent' => $agent, | |
| 144 | + 'business' => $this, | |
| 145 | + ]); | |
| 146 | + } | |
| 147 | + | |
| 148 | + $name = self::cleanFromName($name) ?: self::cleanFromName($this->name); | |
| 149 | + | |
| 150 | + /* | |
| 151 | + * Filter the From name of an email sent from this inbox. | |
| 152 | + * | |
| 153 | + * @since v2.4.1 | |
| 154 | + * @param string $name | |
| 155 | + * @param MailBox $mailbox | |
| 156 | + * @param \FluentSupport\App\Models\Agent|null $agent Set for email an agent wrote. | |
| 157 | + */ | |
| 158 | + return (string) apply_filters('fluent_support/mailbox_from_name', $name, $this, $agent); | |
| 159 | + } | |
| 160 | + | |
| 161 | + // A header value: no line breaks, quotes or angle brackets, no unparsed smartcodes. | |
| 162 | + private static function cleanFromName($name) | |
| 163 | + { | |
| 164 | + $name = preg_replace('/{{[^}]*}}/', '', (string) $name); | |
| 165 | + $name = wp_strip_all_tags($name, true); | |
| 166 | + $name = preg_replace('/[\x00-\x1F\x7F"<>]+/u', ' ', $name); | |
| 167 | + $name = trim(preg_replace('/\s+/u', ' ', $name)); | |
| 168 | + | |
| 169 | + return mb_substr($name, 0, 120); | |
| 170 | + } | |
| 171 | + | |
| 120 | 172 | public function getMeta($key, $default = '') |
| 121 | 173 | { |
| 122 | 174 | $class = __NAMESPACE__ . '\MailBox'; |
| 123 | 175 | |
| @@ -175,9 +227,8 @@ | ||
| 175 | 227 | ->where('object_id', $this->id) |
| 176 | 228 | ->delete(); |
| 177 | 229 | } |
| 178 | 230 | /** |
| 179 | - * This `getMailBox` method is used to get a mailbox by id. | |
| 180 | 231 | * @param int $id |
| 181 | 232 | * @return MailBox |
| 182 | 233 | */ |
| 183 | 234 | public function getMailBox ($id ) |
| @@ -185,9 +236,8 @@ | ||
| 185 | 236 | return MailBox::findOrFail($id); |
| 186 | 237 | } |
| 187 | 238 | |
| 188 | 239 | /** |
| 189 | - * This `createMailBox` method is used to create a new MailBox. | |
| 190 | 240 | * @param array $data |
| 191 | 241 | * @return MailBox |
| 192 | 242 | */ |
| 193 | 243 | public function createMailBox ( $data ) |
| @@ -210,9 +260,8 @@ | ||
| 210 | 260 | } |
| 211 | 261 | |
| 212 | 262 | |
| 213 | 263 | /** |
| 214 | - * This `updateMailBox` method is used to update a mailbox. | |
| 215 | 264 | * @param array $data |
| 216 | 265 | * @param int $mailBoxId |
| 217 | 266 | * @return object $mailbox |
| 218 | 267 | */ |