| 1 |
<?php |
| 2 |
/** |
| 3 |
* Summary email base class |
| 4 |
* |
| 5 |
* @since 6.7 |
| 6 |
* |
| 7 |
* @package Formidable |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
die( 'You are not allowed to call this page directly.' ); |
| 12 |
} |
| 13 |
|
| 14 |
abstract class FrmEmailSummary { |
| 15 |
|
| 16 |
/** |
| 17 |
* Name of the file with the HTML content, without the file extension. |
| 18 |
* |
| 19 |
* @since 6.15 |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $template = 'base'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is HTML email? |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
protected $is_html = true; |
| 31 |
|
| 32 |
/** |
| 33 |
* Gets email subject. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
abstract protected function get_subject(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Gets inner content. |
| 41 |
* |
| 42 |
* @since 6.15 Method isn't abstract anymore, returning an empty string by default. |
| 43 |
* |
| 44 |
* @return false|string |
| 45 |
*/ |
| 46 |
protected function get_inner_content() { |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Constructor. |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
/** |
| 55 |
* Enables HTML for summary emails. |
| 56 |
* |
| 57 |
* @since 6.7 |
| 58 |
* |
| 59 |
* @param bool $enable Set to `true` to enable. |
| 60 |
*/ |
| 61 |
$this->is_html = apply_filters( 'frm_html_summary_emails', true ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Gets recipients. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
protected function get_recipients() { |
| 70 |
$recipients = FrmAppHelper::get_settings()->summary_emails_recipients; |
| 71 |
$recipients = str_replace( '[admin_email]', get_bloginfo( 'admin_email' ), $recipients ); |
| 72 |
FrmEmailSummaryHelper::maybe_remove_recipients_from_api( $recipients ); |
| 73 |
return $recipients; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets email headers. |
| 78 |
* |
| 79 |
* @return string[] |
| 80 |
*/ |
| 81 |
protected function get_headers() { |
| 82 |
return array( |
| 83 |
'Content-Type: ' . ( $this->is_html ? 'text/html; charset=UTF-8' : 'text/plain' ), |
| 84 |
'From: ' . get_bloginfo( 'name' ) . ' <' . FrmEmailHelper::get_default_from_email() . '>', |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets email content. |
| 90 |
* |
| 91 |
* @since 6.8 This can return `false` to skip sending email. |
| 92 |
* |
| 93 |
* @return false|string |
| 94 |
*/ |
| 95 |
protected function get_content() { |
| 96 |
$args = $this->get_content_args(); |
| 97 |
|
| 98 |
if ( false === $args ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
ob_start(); |
| 103 |
include $this->get_include_file( $this->template ); |
| 104 |
$content = ob_get_clean(); |
| 105 |
|
| 106 |
$content = str_replace( '%%INNER_CONTENT%%', $this->get_inner_content(), $content ); |
| 107 |
|
| 108 |
return $content; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets include file path from the given file name. |
| 113 |
* |
| 114 |
* @param string $file_name File name. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
protected function get_include_file( $file_name ) { |
| 119 |
$suffix = $this->is_html ? '' : '-plain'; |
| 120 |
return $this->get_include_folder() . $file_name . $suffix . '.php'; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Gets the full path to the folder containing the email templates. |
| 125 |
* |
| 126 |
* @since 6.15 |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
protected function get_include_folder() { |
| 131 |
return FrmAppHelper::plugin_path() . '/classes/views/summary-emails/'; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Gets content args. |
| 136 |
* |
| 137 |
* @since 6.8 This can return `false` to skip sending email. |
| 138 |
* |
| 139 |
* @return array|false |
| 140 |
*/ |
| 141 |
protected function get_content_args() { |
| 142 |
$args = array( |
| 143 |
'subject' => $this->get_subject(), |
| 144 |
'site_url' => home_url( '/' ), |
| 145 |
'site_url_display' => home_url( '/' ), |
| 146 |
'unsubscribe_url' => site_url() . '/wp-admin/admin.php?page=formidable-settings&t=misc_settings', |
| 147 |
'support_url' => FrmEmailSummaryHelper::get_frm_url( 'new-topic', 'contact_support' ), |
| 148 |
); |
| 149 |
|
| 150 |
/** |
| 151 |
* Filters the summary email content args. |
| 152 |
* |
| 153 |
* @since 6.7 |
| 154 |
* |
| 155 |
* @param array $args Content args. |
| 156 |
* @param array $filter_args Contains `email_obj`: summary email object. |
| 157 |
*/ |
| 158 |
return apply_filters( 'frm_summary_email_content_args', $args, array( 'email_obj' => $this ) ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Check if the email should be sent. |
| 163 |
* |
| 164 |
* @since 6.15 |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
protected function should_send() { |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Sends email. |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function send() { |
| 178 |
if ( ! $this->should_send() ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
$recipients = $this->get_recipients(); |
| 183 |
|
| 184 |
if ( ! $recipients ) { |
| 185 |
// Return true to not try to send this email on the next day. |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
$content = $this->get_content(); |
| 190 |
|
| 191 |
if ( false === $content ) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
$subject = $this->get_subject(); |
| 196 |
$headers = $this->get_headers(); |
| 197 |
|
| 198 |
$sent = wp_mail( $recipients, $subject, $content, $headers ); |
| 199 |
|
| 200 |
if ( ! $sent ) { |
| 201 |
$headers = implode( "\r\n", $headers ); |
| 202 |
$sent = mail( $recipients, $subject, $content, $headers ); |
| 203 |
} |
| 204 |
|
| 205 |
return $sent; |
| 206 |
} |
| 207 |
} |
| 208 |
|