Plugin Directory

Changeset 2596119


Ignore:
Timestamp:
09/09/2021 10:18:38 AM (5 years ago)
Author:
juvodesign
Message:

Update to version 2.0.8 from GitHub

Location:
juvo-mail-editor
Files:
4 added
16 edited
1 copied

Legend:

Unmodified
Added
Removed
  • juvo-mail-editor/tags/2.0.8/juvo-mail-editor.php

    r2595821 r2596119  
    88 * Text Domain:     juvo-mail-editor
    99 * Domain Path:     /languages
    10  * Version:         2.0.7
     10 * Version:         2.0.8
    1111 */
    1212
  • juvo-mail-editor/tags/2.0.8/readme.txt

    r2595821 r2596119  
    44License: GPLv2 or later
    55Tested up to: 5.8
    6 Stable tag: 2.0.7
     6Stable tag: 2.0.8
    77
    88JUVO Mail Editor helps to modify the standard WordPress Mailings and allows adding dynamic mail triggers.
  • juvo-mail-editor/tags/2.0.8/src/Integrations/BuddyBoss.php

    r2578743 r2596119  
    55class BuddyBoss {
    66
    7     public function useTemplate( string $content, string $trigger, $context ) {
     7    public function useTemplate( array $args ): array {
    88        if ( function_exists( 'bp_is_active' ) ) {
    9             /** @phpstan-ignore-next-line */
    10             return bp_email_core_wp_get_template( $content, $context );
     9            // BoddyBoss might have already applied the template
     10            if ( isset( $args["message"] ) && strpos( $args["message"], '<!DOCTYPE html>' ) === false ) {
     11                /** @phpstan-ignore-next-line */
     12                $args["message"] = bp_email_core_wp_get_template( $args["message"] );
     13            }
    1114        }
    1215
    13         return $content;
     16        return $args;
    1417    }
    1518
  • juvo-mail-editor/tags/2.0.8/src/Mail_Editor.php

    r2587510 r2596119  
    1111use JUVO_MailEditor\Mails\New_User_Admin_Rest;
    1212use JUVO_MailEditor\Mails\New_User_Rest;
     13use JUVO_MailEditor\Mails\Password_Changed;
     14use JUVO_MailEditor\Mails\Password_Changed_Admin;
    1315use JUVO_MailEditor\Mails\Password_Reset;
    1416use JUVO_MailEditor\Mails\Password_Reset_Admin;
     
    152154        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordReset, "registerTrigger" );
    153155        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordReset, "addCustomFields" );
    154 
    155156        $this->loader->add_filter( 'retrieve_password_message', $passwordReset, 'password_reset_email_message', 10, 4 );
    156         $this->loader->add_filter( 'retrieve_password_title', $passwordReset, 'password_reset_email_subject', 10, 4 );
    157157
    158158        /**
     
    162162        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordResetAdmin, "registerTrigger" );
    163163        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordResetAdmin, "addCustomFields" );
    164 
    165164        $this->loader->add_filter( "retrieve_password_message", $passwordResetAdmin, "password_reset_email_admin", 99, 4 );
    166165
     166        /**
     167         * Password Changed
     168         */
     169        $passwordChanged = new Password_Changed();
     170        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordChanged, "registerTrigger" );
     171        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordChanged, "addCustomFields" );
     172        $this->loader->add_filter( 'password_change_email', $passwordChanged, 'password_changed_email', 10, 2 );
     173
     174        /**
     175         * Password Changed Admin
     176         */
     177        $passwordChangedAdmin = new Password_Changed_Admin();
     178        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordChangedAdmin, "registerTrigger" );
     179        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordChangedAdmin, "addCustomFields" );
     180        $this->loader->add_filter( 'wp_password_change_notification_email', $passwordChangedAdmin, 'password_changed_admin_email', 10, 2 );
     181
    167182
    168183        /**
    169184         * Integrations
    170185         */
    171         $this->loader->add_filter( "juvo_mail_editor_after_content_placeholder", new BuddyBoss(), "useTemplate", 10, 3 );
    172 
     186        $this->loader->add_filter( "wp_mail", new BuddyBoss(), "useTemplate", 99, 1 );
    173187    }
    174188
  • juvo-mail-editor/tags/2.0.8/src/Mails/New_User.php

    r2595821 r2596119  
    2525    public function new_user_notification_email( array $email, WP_User $user ): array {
    2626
    27         // Add Muted Capability
    28         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    29             return [];
    30         }
    31 
    3227        $this->setPlaceholderValues( $user );
    3328
    34         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    35         $template = $relay->getPosts();
     29        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     30        $relay->sendMails();
    3631
    37         if ( ! empty( $template ) ) {
    38             $email["to"]      = $relay->prepareRecipients( $template[0] );
    39             $email["subject"] = $relay->prepareSubject( $template[0] );
    40             $email["message"] = $relay->prepareContent( $template[0] );
    41         }
    42 
    43         return $email;
     32        return [];
    4433    }
    4534
  • juvo-mail-editor/tags/2.0.8/src/Mails/New_User_Admin.php

    r2595821 r2596119  
    1515    function new_user_notification_email_admin( array $email, WP_User $user ): array {
    1616
    17         // Add Muted Capability
    18         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    19             return [];
    20         }
    21 
    2217        $this->setPlaceholderValues( $user );
    2318
    24         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    25         $template = $relay->getPosts();
     19        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     20        $relay->sendMails();
    2621
    27         if ( ! empty( $template ) ) {
    28             $email["to"]      = $relay->prepareRecipients( $template[0] );
    29             $email["subject"] = $relay->prepareSubject( $template[0] );
    30             $email["message"] = $relay->prepareContent( $template[0] );
    31         }
    32 
    33         return $email;
     22        return [];
    3423    }
    3524
  • juvo-mail-editor/tags/2.0.8/src/Mails/Password_Reset.php

    r2594645 r2596119  
    1919    ];
    2020
     21    /**
     22     * @param string $message
     23     * @param string $key
     24     * @param string $user_login
     25     * @param WP_User $user
     26     *
     27     * @return string Empty string to prevent default wordpress mail from being sent
     28     */
    2129    function password_reset_email_message( string $message, string $key, string $user_login, WP_User $user ): string {
    22 
    23         // Add Muted Capability
    24         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    25             return "";
    26         }
    2730
    2831        $this->setPlaceholderValues( $user, [ "key" => $key ] );
    2932
    30         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    31         $template = $relay->getPosts();
     33        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     34        $relay->sendMails();
    3235
    33         if ( ! empty( $template ) ) {
    34             return $relay->prepareContent( $template[0] );
    35         } else {
    36             return $relay->prepareContent();
    37         }
     36        return "";
    3837
    3938    }
     
    4746    public function getTrigger(): string {
    4847        return "password_reset";
    49     }
    50 
    51     function password_reset_email_subject( string $title, string $user_login, WP_User $user ): string {
    52         $this->setPlaceholderValues( $user );
    53         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    54         $template = $relay->getPosts();
    55 
    56         if ( ! empty( $template ) ) {
    57             return $relay->prepareSubject( $template[0] );
    58         } else {
    59             return $relay->prepareSubject();
    60         }
    61 
    62 
    6348    }
    6449
  • juvo-mail-editor/tags/2.0.8/src/Relay.php

    r2595821 r2596119  
    5050
    5151    /**
    52      * Sends mails for all posts that are associated with the trigger.
    53      * To send mails even if not post is associated set the "alwaysSend" flag.
    54      *
    55      * If the flag is set the term defaults are used for the mailing
    56      */
    57     public function sendMails() {
    58 
    59         // Add Muted Capability
    60         if ( self::triggerIsMuted( $this->trigger ) ) {
    61             return false;
    62         }
    63 
    64         if ( ! empty( $this->posts ) ) {
    65             foreach ( $this->posts as $post ) {
    66 
    67                 // Content
    68                 $content = $this->prepareContent( $post );
    69 
    70                 // Subject
    71                 $subject = $this->prepareSubject( $post );
    72 
    73                 // Recipients
    74                 $recipients = $this->prepareRecipients( $post );
    75 
    76                 $mail = new Generic( $subject, $content, $recipients );
    77                 $mail->send();
    78 
    79             }
    80         } else {
    81 
    82             // Some triggers might only send mails if a post is associated
    83             $alwaysSent = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_always_send", true );
    84             if ( ! $alwaysSent ) {
    85                 return false;
    86             }
    87 
    88             /// Fallback if not posts are found for configured trigger
    89             $content    = $this->prepareContent();
    90             $subject    = $this->prepareSubject();
    91             $recipients = $this->prepareRecipients();
    92 
    93             $mail = new Generic( $subject, $content, $recipients );
    94             $mail->send();
    95         }
    96 
    97         return true;
    98     }
    99 
    100     /**
    101      * @return array
    102      */
    103     public function getPlaceholders(): array {
    104         return $this->placeholders;
    105     }
    106 
    107     /**
    108      * @return WP_Term
    109      */
    110     public function getTerm() {
    111         return $this->term;
    112     }
    113 
    114     /**
    115      * @return WP_Post[]
    116      */
    117     public function getPosts(): array {
    118         return $this->posts;
     52     * @return false|WP_Term
     53     */
     54    private function setTerm() {
     55        return get_term_by( "slug", $this->trigger, Mail_Trigger_TAX::TAXONOMY_NAME );
    11956    }
    12057
     
    14077
    14178    /**
    142      * @return false|WP_Term
    143      */
    144     private function setTerm() {
    145         return get_term_by( "slug", $this->trigger, Mail_Trigger_TAX::TAXONOMY_NAME );
     79     * @param array $placeholders
     80     *
     81     * @return array
     82     */
     83    public function preparePlaceholders( array $placeholders ): array {
     84        $defaultPlaceholders = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_placeholders", true );
     85
     86        if ( ! $defaultPlaceholders ) {
     87            return $defaultPlaceholders;
     88        }
     89
     90        return $placeholders + $defaultPlaceholders;
     91    }
     92
     93    /**
     94     * Sends mails for all posts that are associated with the trigger.
     95     * To send mails even if not post is associated set the "alwaysSend" flag.
     96     *
     97     * If the flag is set the term defaults are used for the mailing
     98     */
     99    public function sendMails() {
     100
     101        // Add Muted Capability
     102        if ( self::triggerIsMuted( $this->trigger ) ) {
     103            return false;
     104        }
     105
     106        if ( ! empty( $this->posts ) ) {
     107            foreach ( $this->posts as $post ) {
     108
     109                // Content
     110                $content = $this->prepareContent( $post );
     111
     112                // Subject
     113                $subject = $this->prepareSubject( $post );
     114
     115                // Recipients
     116                $recipients = $this->prepareRecipients( $post );
     117
     118                $mail = new Generic( $subject, $content, $recipients );
     119                $mail->send();
     120
     121            }
     122        } else {
     123
     124            // Some triggers might only send mails if a post is associated
     125            $alwaysSent = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_always_send", true );
     126            if ( ! $alwaysSent ) {
     127                return false;
     128            }
     129
     130            /// Fallback if not posts are found for configured trigger
     131            $content    = $this->prepareContent();
     132            $subject    = $this->prepareSubject();
     133            $recipients = $this->prepareRecipients();
     134
     135            $mail = new Generic( $subject, $content, $recipients );
     136            $mail->send();
     137        }
     138
     139        return true;
     140    }
     141
     142    /**
     143     * Checks if the given trigger is globally muted.
     144     * Might be called manually if the mail is not send using the 'Relay' class but eg. with a filter.
     145     *
     146     * @param string $trigger
     147     *
     148     * @return bool
     149     */
     150    public static function triggerIsMuted( string $trigger ): bool {
     151        $pluginSettings = get_option( 'settings' );
     152
     153        if ( isset( $pluginSettings['trigger_mute'] ) ) {
     154            return in_array( $trigger, $pluginSettings['trigger_mute'] );
     155        }
     156
     157        return false;
    146158    }
    147159
     
    168180        $content = apply_filters( "juvo_mail_editor_after_content_placeholder", $content, $this->trigger, $this->context );
    169181
    170         $this->setContentType( $content );
     182        $content = $this->setContentType( $content );
    171183
    172184        return $content;
     185    }
     186
     187    public function setContentType( string $message ): string {
     188
     189        $type    = "text/html";
     190        $message = wpautop( $message );
     191
     192        add_filter( 'wp_mail_content_type', function( $content_type ) use ( $type ) {
     193            return $type;
     194        }, 10 );
     195
     196        return $message;
    173197    }
    174198
     
    212236
    213237    /**
    214      * @param array $placeholders
    215      *
    216238     * @return array
    217239     */
    218     public function preparePlaceholders( array $placeholders ): array {
    219         $defaultPlaceholders = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_placeholders", true );
    220 
    221         if ( ! $defaultPlaceholders ) {
    222             return $defaultPlaceholders;
    223         }
    224 
    225         return $placeholders + $defaultPlaceholders;
    226     }
    227 
    228     public function setContentType( string $message ): string {
    229 
    230         $type = 'text/plain';
    231 
    232         if ( $message != strip_tags( $message ) ) {
    233             $type    = "text/html";
    234             $message = wpautop( $message );
    235         }
    236 
    237         add_filter( 'wp_mail_content_type', function( $content_type ) use ( $type ) {
    238             return $type;
    239         }, 10 );
    240 
    241         return $message;
    242     }
    243 
    244     /**
    245      * Checks if the given trigger is globally muted.
    246      * Might be called manually if the mail is not send using the 'Relay' class but eg. with a filter.
    247      *
    248      * @param string $trigger
    249      *
    250      * @return bool
    251      */
    252     public static function triggerIsMuted( string $trigger ): bool {
    253         $pluginSettings = get_option( 'settings' );
    254 
    255         if ( isset( $pluginSettings['trigger_mute'] ) ) {
    256             $mutedTriggers = $pluginSettings['trigger_mute'];
    257 
    258             return in_array( $trigger, $mutedTriggers );
    259         }
    260 
    261         return false;
     240    public function getPlaceholders(): array {
     241        return $this->placeholders;
     242    }
     243
     244    /**
     245     * @return WP_Term
     246     */
     247    public function getTerm() {
     248        return $this->term;
     249    }
     250
     251    /**
     252     * @return WP_Post[]
     253     */
     254    public function getPosts(): array {
     255        return $this->posts;
    262256    }
    263257
  • juvo-mail-editor/trunk/juvo-mail-editor.php

    r2595821 r2596119  
    88 * Text Domain:     juvo-mail-editor
    99 * Domain Path:     /languages
    10  * Version:         2.0.7
     10 * Version:         2.0.8
    1111 */
    1212
  • juvo-mail-editor/trunk/readme.txt

    r2595821 r2596119  
    44License: GPLv2 or later
    55Tested up to: 5.8
    6 Stable tag: 2.0.7
     6Stable tag: 2.0.8
    77
    88JUVO Mail Editor helps to modify the standard WordPress Mailings and allows adding dynamic mail triggers.
  • juvo-mail-editor/trunk/src/Integrations/BuddyBoss.php

    r2578743 r2596119  
    55class BuddyBoss {
    66
    7     public function useTemplate( string $content, string $trigger, $context ) {
     7    public function useTemplate( array $args ): array {
    88        if ( function_exists( 'bp_is_active' ) ) {
    9             /** @phpstan-ignore-next-line */
    10             return bp_email_core_wp_get_template( $content, $context );
     9            // BoddyBoss might have already applied the template
     10            if ( isset( $args["message"] ) && strpos( $args["message"], '<!DOCTYPE html>' ) === false ) {
     11                /** @phpstan-ignore-next-line */
     12                $args["message"] = bp_email_core_wp_get_template( $args["message"] );
     13            }
    1114        }
    1215
    13         return $content;
     16        return $args;
    1417    }
    1518
  • juvo-mail-editor/trunk/src/Mail_Editor.php

    r2587510 r2596119  
    1111use JUVO_MailEditor\Mails\New_User_Admin_Rest;
    1212use JUVO_MailEditor\Mails\New_User_Rest;
     13use JUVO_MailEditor\Mails\Password_Changed;
     14use JUVO_MailEditor\Mails\Password_Changed_Admin;
    1315use JUVO_MailEditor\Mails\Password_Reset;
    1416use JUVO_MailEditor\Mails\Password_Reset_Admin;
     
    152154        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordReset, "registerTrigger" );
    153155        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordReset, "addCustomFields" );
    154 
    155156        $this->loader->add_filter( 'retrieve_password_message', $passwordReset, 'password_reset_email_message', 10, 4 );
    156         $this->loader->add_filter( 'retrieve_password_title', $passwordReset, 'password_reset_email_subject', 10, 4 );
    157157
    158158        /**
     
    162162        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordResetAdmin, "registerTrigger" );
    163163        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordResetAdmin, "addCustomFields" );
    164 
    165164        $this->loader->add_filter( "retrieve_password_message", $passwordResetAdmin, "password_reset_email_admin", 99, 4 );
    166165
     166        /**
     167         * Password Changed
     168         */
     169        $passwordChanged = new Password_Changed();
     170        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordChanged, "registerTrigger" );
     171        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordChanged, "addCustomFields" );
     172        $this->loader->add_filter( 'password_change_email', $passwordChanged, 'password_changed_email', 10, 2 );
     173
     174        /**
     175         * Password Changed Admin
     176         */
     177        $passwordChangedAdmin = new Password_Changed_Admin();
     178        $this->loader->add_filter( "juvo_mail_editor_trigger", $passwordChangedAdmin, "registerTrigger" );
     179        $this->loader->add_filter( "juvo_mail_editor_post_metabox", $passwordChangedAdmin, "addCustomFields" );
     180        $this->loader->add_filter( 'wp_password_change_notification_email', $passwordChangedAdmin, 'password_changed_admin_email', 10, 2 );
     181
    167182
    168183        /**
    169184         * Integrations
    170185         */
    171         $this->loader->add_filter( "juvo_mail_editor_after_content_placeholder", new BuddyBoss(), "useTemplate", 10, 3 );
    172 
     186        $this->loader->add_filter( "wp_mail", new BuddyBoss(), "useTemplate", 99, 1 );
    173187    }
    174188
  • juvo-mail-editor/trunk/src/Mails/New_User.php

    r2595821 r2596119  
    2525    public function new_user_notification_email( array $email, WP_User $user ): array {
    2626
    27         // Add Muted Capability
    28         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    29             return [];
    30         }
    31 
    3227        $this->setPlaceholderValues( $user );
    3328
    34         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    35         $template = $relay->getPosts();
     29        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     30        $relay->sendMails();
    3631
    37         if ( ! empty( $template ) ) {
    38             $email["to"]      = $relay->prepareRecipients( $template[0] );
    39             $email["subject"] = $relay->prepareSubject( $template[0] );
    40             $email["message"] = $relay->prepareContent( $template[0] );
    41         }
    42 
    43         return $email;
     32        return [];
    4433    }
    4534
  • juvo-mail-editor/trunk/src/Mails/New_User_Admin.php

    r2595821 r2596119  
    1515    function new_user_notification_email_admin( array $email, WP_User $user ): array {
    1616
    17         // Add Muted Capability
    18         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    19             return [];
    20         }
    21 
    2217        $this->setPlaceholderValues( $user );
    2318
    24         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    25         $template = $relay->getPosts();
     19        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     20        $relay->sendMails();
    2621
    27         if ( ! empty( $template ) ) {
    28             $email["to"]      = $relay->prepareRecipients( $template[0] );
    29             $email["subject"] = $relay->prepareSubject( $template[0] );
    30             $email["message"] = $relay->prepareContent( $template[0] );
    31         }
    32 
    33         return $email;
     22        return [];
    3423    }
    3524
  • juvo-mail-editor/trunk/src/Mails/Password_Reset.php

    r2594645 r2596119  
    1919    ];
    2020
     21    /**
     22     * @param string $message
     23     * @param string $key
     24     * @param string $user_login
     25     * @param WP_User $user
     26     *
     27     * @return string Empty string to prevent default wordpress mail from being sent
     28     */
    2129    function password_reset_email_message( string $message, string $key, string $user_login, WP_User $user ): string {
    22 
    23         // Add Muted Capability
    24         if ( Relay::triggerIsMuted( $this->getTrigger() ) ) {
    25             return "";
    26         }
    2730
    2831        $this->setPlaceholderValues( $user, [ "key" => $key ] );
    2932
    30         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    31         $template = $relay->getPosts();
     33        $relay = new Relay( $this->getTrigger(), $this->placeholders, $user );
     34        $relay->sendMails();
    3235
    33         if ( ! empty( $template ) ) {
    34             return $relay->prepareContent( $template[0] );
    35         } else {
    36             return $relay->prepareContent();
    37         }
     36        return "";
    3837
    3938    }
     
    4746    public function getTrigger(): string {
    4847        return "password_reset";
    49     }
    50 
    51     function password_reset_email_subject( string $title, string $user_login, WP_User $user ): string {
    52         $this->setPlaceholderValues( $user );
    53         $relay    = new Relay( $this->getTrigger(), $this->placeholders, $user );
    54         $template = $relay->getPosts();
    55 
    56         if ( ! empty( $template ) ) {
    57             return $relay->prepareSubject( $template[0] );
    58         } else {
    59             return $relay->prepareSubject();
    60         }
    61 
    62 
    6348    }
    6449
  • juvo-mail-editor/trunk/src/Relay.php

    r2595821 r2596119  
    5050
    5151    /**
    52      * Sends mails for all posts that are associated with the trigger.
    53      * To send mails even if not post is associated set the "alwaysSend" flag.
    54      *
    55      * If the flag is set the term defaults are used for the mailing
    56      */
    57     public function sendMails() {
    58 
    59         // Add Muted Capability
    60         if ( self::triggerIsMuted( $this->trigger ) ) {
    61             return false;
    62         }
    63 
    64         if ( ! empty( $this->posts ) ) {
    65             foreach ( $this->posts as $post ) {
    66 
    67                 // Content
    68                 $content = $this->prepareContent( $post );
    69 
    70                 // Subject
    71                 $subject = $this->prepareSubject( $post );
    72 
    73                 // Recipients
    74                 $recipients = $this->prepareRecipients( $post );
    75 
    76                 $mail = new Generic( $subject, $content, $recipients );
    77                 $mail->send();
    78 
    79             }
    80         } else {
    81 
    82             // Some triggers might only send mails if a post is associated
    83             $alwaysSent = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_always_send", true );
    84             if ( ! $alwaysSent ) {
    85                 return false;
    86             }
    87 
    88             /// Fallback if not posts are found for configured trigger
    89             $content    = $this->prepareContent();
    90             $subject    = $this->prepareSubject();
    91             $recipients = $this->prepareRecipients();
    92 
    93             $mail = new Generic( $subject, $content, $recipients );
    94             $mail->send();
    95         }
    96 
    97         return true;
    98     }
    99 
    100     /**
    101      * @return array
    102      */
    103     public function getPlaceholders(): array {
    104         return $this->placeholders;
    105     }
    106 
    107     /**
    108      * @return WP_Term
    109      */
    110     public function getTerm() {
    111         return $this->term;
    112     }
    113 
    114     /**
    115      * @return WP_Post[]
    116      */
    117     public function getPosts(): array {
    118         return $this->posts;
     52     * @return false|WP_Term
     53     */
     54    private function setTerm() {
     55        return get_term_by( "slug", $this->trigger, Mail_Trigger_TAX::TAXONOMY_NAME );
    11956    }
    12057
     
    14077
    14178    /**
    142      * @return false|WP_Term
    143      */
    144     private function setTerm() {
    145         return get_term_by( "slug", $this->trigger, Mail_Trigger_TAX::TAXONOMY_NAME );
     79     * @param array $placeholders
     80     *
     81     * @return array
     82     */
     83    public function preparePlaceholders( array $placeholders ): array {
     84        $defaultPlaceholders = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_placeholders", true );
     85
     86        if ( ! $defaultPlaceholders ) {
     87            return $defaultPlaceholders;
     88        }
     89
     90        return $placeholders + $defaultPlaceholders;
     91    }
     92
     93    /**
     94     * Sends mails for all posts that are associated with the trigger.
     95     * To send mails even if not post is associated set the "alwaysSend" flag.
     96     *
     97     * If the flag is set the term defaults are used for the mailing
     98     */
     99    public function sendMails() {
     100
     101        // Add Muted Capability
     102        if ( self::triggerIsMuted( $this->trigger ) ) {
     103            return false;
     104        }
     105
     106        if ( ! empty( $this->posts ) ) {
     107            foreach ( $this->posts as $post ) {
     108
     109                // Content
     110                $content = $this->prepareContent( $post );
     111
     112                // Subject
     113                $subject = $this->prepareSubject( $post );
     114
     115                // Recipients
     116                $recipients = $this->prepareRecipients( $post );
     117
     118                $mail = new Generic( $subject, $content, $recipients );
     119                $mail->send();
     120
     121            }
     122        } else {
     123
     124            // Some triggers might only send mails if a post is associated
     125            $alwaysSent = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_always_send", true );
     126            if ( ! $alwaysSent ) {
     127                return false;
     128            }
     129
     130            /// Fallback if not posts are found for configured trigger
     131            $content    = $this->prepareContent();
     132            $subject    = $this->prepareSubject();
     133            $recipients = $this->prepareRecipients();
     134
     135            $mail = new Generic( $subject, $content, $recipients );
     136            $mail->send();
     137        }
     138
     139        return true;
     140    }
     141
     142    /**
     143     * Checks if the given trigger is globally muted.
     144     * Might be called manually if the mail is not send using the 'Relay' class but eg. with a filter.
     145     *
     146     * @param string $trigger
     147     *
     148     * @return bool
     149     */
     150    public static function triggerIsMuted( string $trigger ): bool {
     151        $pluginSettings = get_option( 'settings' );
     152
     153        if ( isset( $pluginSettings['trigger_mute'] ) ) {
     154            return in_array( $trigger, $pluginSettings['trigger_mute'] );
     155        }
     156
     157        return false;
    146158    }
    147159
     
    168180        $content = apply_filters( "juvo_mail_editor_after_content_placeholder", $content, $this->trigger, $this->context );
    169181
    170         $this->setContentType( $content );
     182        $content = $this->setContentType( $content );
    171183
    172184        return $content;
     185    }
     186
     187    public function setContentType( string $message ): string {
     188
     189        $type    = "text/html";
     190        $message = wpautop( $message );
     191
     192        add_filter( 'wp_mail_content_type', function( $content_type ) use ( $type ) {
     193            return $type;
     194        }, 10 );
     195
     196        return $message;
    173197    }
    174198
     
    212236
    213237    /**
    214      * @param array $placeholders
    215      *
    216238     * @return array
    217239     */
    218     public function preparePlaceholders( array $placeholders ): array {
    219         $defaultPlaceholders = get_term_meta( $this->term->term_id, Mail_Trigger_TAX::TAXONOMY_NAME . "_placeholders", true );
    220 
    221         if ( ! $defaultPlaceholders ) {
    222             return $defaultPlaceholders;
    223         }
    224 
    225         return $placeholders + $defaultPlaceholders;
    226     }
    227 
    228     public function setContentType( string $message ): string {
    229 
    230         $type = 'text/plain';
    231 
    232         if ( $message != strip_tags( $message ) ) {
    233             $type    = "text/html";
    234             $message = wpautop( $message );
    235         }
    236 
    237         add_filter( 'wp_mail_content_type', function( $content_type ) use ( $type ) {
    238             return $type;
    239         }, 10 );
    240 
    241         return $message;
    242     }
    243 
    244     /**
    245      * Checks if the given trigger is globally muted.
    246      * Might be called manually if the mail is not send using the 'Relay' class but eg. with a filter.
    247      *
    248      * @param string $trigger
    249      *
    250      * @return bool
    251      */
    252     public static function triggerIsMuted( string $trigger ): bool {
    253         $pluginSettings = get_option( 'settings' );
    254 
    255         if ( isset( $pluginSettings['trigger_mute'] ) ) {
    256             $mutedTriggers = $pluginSettings['trigger_mute'];
    257 
    258             return in_array( $trigger, $mutedTriggers );
    259         }
    260 
    261         return false;
     240    public function getPlaceholders(): array {
     241        return $this->placeholders;
     242    }
     243
     244    /**
     245     * @return WP_Term
     246     */
     247    public function getTerm() {
     248        return $this->term;
     249    }
     250
     251    /**
     252     * @return WP_Post[]
     253     */
     254    public function getPosts(): array {
     255        return $this->posts;
    262256    }
    263257
Note: See TracChangeset for help on using the changeset viewer.