Skip to content
Open
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
14 changes: 14 additions & 0 deletions resources/views/widget.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{!! $message !!}

@if ($config && $config->isNotEmpty())
<table class="widefat striped">
<tbody>
@foreach ($config as $key => $value)
<tr>
<th><strong>{{ $key }}</strong></th>
<td>{{ $value }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
11 changes: 9 additions & 2 deletions src/AcornMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class AcornMailServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->singleton('Roots\AcornMail', fn () => AcornMail::make($this->app));
$this->app->singleton(AcornMail::class, fn () => AcornMail::make($this->app));
$this->app->singleton(Widget::class, fn () => Widget::make($this->app));
}

/**
Expand All @@ -23,13 +24,19 @@ public function register()
*/
public function boot()
{
$this->loadViewsFrom(
__DIR__.'/../resources/views',
'AcornMail',
);

if ($this->app->runningInConsole()) {
$this->commands([
Console\Commands\MailConfigCommand::class,
Console\Commands\MailTestCommand::class,
]);
}

$this->app->make('Roots\AcornMail');
$this->app->make(AcornMail::class);
$this->app->make(Widget::class);
}
}
3 changes: 2 additions & 1 deletion src/Console/Commands/MailTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Roots\AcornMail\AcornMail;

class MailTestCommand extends Command
{
Expand Down Expand Up @@ -47,7 +48,7 @@ class MailTestCommand extends Command
*/
public function handle()
{
$package = app('Roots\AcornMail');
$package = app()->make(AcornMail::class);

if (! $package->configured()) {
$this->components->error('The mail SMTP configuration is not set.');
Expand Down
74 changes: 74 additions & 0 deletions src/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Roots\AcornMail;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Roots\Acorn\Application;

class Widget
{
/**
* The mail configuration.
*/
protected Collection $config;

/**
* The AcornMail instance.
*/
private AcornMail $acornMail;

/**
* Instantiate the Acorn Mail Widget.
*/
public function __construct(private Application $app)
{
$this->acornMail = $this->app->make(AcornMail::class);
$this->config = Collection::make($this->app->config->get('mail.mailers.smtp'))
->merge($this->app->config->get('mail.from'));

add_action('wp_dashboard_setup', [$this, 'add']);
}

/**
* Make a new instance of Acorn Mail Widget.
*/
public static function make(Application $app): self
{
return new static($app);
}

/**
* Add the Acorn Mail widget to the WordPress dashboard.
*/
public function add()
{
wp_add_dashboard_widget('acorn_mail_widget', 'Acorn Mail', [$this, 'content']);
}

/**
* Render the Acorn Mail widget content.
*/
public function content()
{
if (! $this->acornMail->configured()) {

echo view('AcornMail::widget', [
'message' => wpautop('Acorn mail is <strong>not</strong> configured and is mimicking out-of-the-box WordPress email delivery.'),
'config' => null,
]);

return;
}

$config = collect($this->config)
->map(fn ($value, $key) => $key === 'password' ? Str::mask($value, '*', 0) : $value)
->mapWithKeys(fn ($value, $key) => [Str::title($key) => $value])
->filter();

echo view('AcornMail::widget', [
'message' => wpautop('Acorn mail is configured and will use SMTP for email delivery.'),
'config' => $config,
]);
}
}