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