From 4ffdc83769e1fb88eea83729e030952fd14cb388 Mon Sep 17 00:00:00 2001
From: Piotr Potrebka <piotr.potrebka@wpdesk.net>
Date: Mon, 23 Jan 2023 14:01:15 +0100
Subject: [PATCH] init

---
 src/Abstracts/ConditionInterface.php   | 12 ++++++++++++
 src/Abstracts/EmailGettersAbstract.php |  6 +++++-
 src/EmailSender.php                    | 13 ++++++++++---
 3 files changed, 27 insertions(+), 4 deletions(-)
 create mode 100644 src/Abstracts/ConditionInterface.php

diff --git a/src/Abstracts/ConditionInterface.php b/src/Abstracts/ConditionInterface.php
new file mode 100644
index 0000000..46cbf9f
--- /dev/null
+++ b/src/Abstracts/ConditionInterface.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace WPDesk\Library\WPEmail\Abstracts;
+
+use WPDesk\View\Renderer\Renderer;
+
+interface ConditionInterface {
+
+
+    public function is_valid(): bool;
+
+}
diff --git a/src/Abstracts/EmailGettersAbstract.php b/src/Abstracts/EmailGettersAbstract.php
index a510234..2c1fc7a 100644
--- a/src/Abstracts/EmailGettersAbstract.php
+++ b/src/Abstracts/EmailGettersAbstract.php
@@ -4,7 +4,7 @@ namespace WPDesk\Library\WPEmail\Abstracts;
 
 use WPDesk\View\Renderer\Renderer;
 
-abstract class EmailGettersAbstract implements EmailGettersInterface, EmailSettersInterface {
+abstract class EmailGettersAbstract implements EmailGettersInterface, ConditionInterface {
 
     /**
      * @var Renderer
@@ -142,4 +142,8 @@ abstract class EmailGettersAbstract implements EmailGettersInterface, EmailSette
         return $this->renderer->render( dirname( __DIR__ ) . '/html/default.php', [ 'content' => $this->get_content(), 'heading' => $this->get_heading(), 'object' => $this->get_object() ] );
     }
 
+    public function is_valid(): bool {
+        return true;
+    }
+
 }
diff --git a/src/EmailSender.php b/src/EmailSender.php
index 2f5ea76..3511483 100644
--- a/src/EmailSender.php
+++ b/src/EmailSender.php
@@ -2,6 +2,7 @@
 
 namespace WPDesk\Library\WPEmail\Emails;
 
+use WPDesk\Library\WPEmail\Abstracts\ConditionInterface;
 use WPDesk\Library\WPEmail\Abstracts\EmailGettersInterface;
 
 class EmailSender {
@@ -81,9 +82,15 @@ class EmailSender {
     public function send() {
         foreach ( $this->get_emails() as $email ) {
             $this->before_wp_mail();
-            wp_mail(
-                $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments()
-            );
+
+            if( $email instanceof ConditionInterface ) {
+                if( $email->is_valid() ) {
+                    wp_mail(
+                        $email->get_recipients(), $email->get_subject(), $email->render(), $email->get_headers(), $email->get_attachments()
+                    );
+                }
+            }
+
             $this->after_wp_mail();
         }
     }
-- 
GitLab