Skip to content

Commit

Permalink
✨ feat: Added dashboard widget with acorn mail configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tombroucke committed Sep 26, 2024
1 parent 797ba56 commit effc4ca
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions src/AcornMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AcornMailServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton(AcornMail::class, fn () => AcornMail::make($this->app));
$this->app->singleton(Widget::class, fn () => Widget::make($this->app));
}

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

if ($this->app->runningInConsole()) {
$this->commands([
Console\Commands\MailConfigCommand::class,
Expand All @@ -31,5 +37,6 @@ public function boot()
}

$this->app->make(AcornMail::class);
$this->app->make(Widget::class);
}
}
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,
]);
}
}

0 comments on commit effc4ca

Please sign in to comment.