| 1 |
<?php |
| 2 |
/** |
| 3 |
* Emails: Abstract |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @copyright Copyright (c) 2023, Sandhills Development, LLC |
| 7 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 8 |
* @since 4.7.3 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SimplePay\Core\Emails\Email; |
| 12 |
|
| 13 |
use SimplePay\Core\Emails\Template\DefaultTemplate; |
| 14 |
use SimplePay\Core\Emails\Template\PlainTemplate; |
| 15 |
use SimplePay\Core\NotificationInbox\NotificationAwareInterface; |
| 16 |
use SimplePay\Core\NotificationInbox\NotificationAwareTrait; |
| 17 |
use SimplePay\Core\NotificationInbox\NotificationRepository; |
| 18 |
use SimplePay\Core\Settings; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Email class. |
| 26 |
* |
| 27 |
* @since 4.7.3 |
| 28 |
*/ |
| 29 |
abstract class AbstractEmail implements EmailInterface, NotificationAwareInterface { |
| 30 |
|
| 31 |
use NotificationAwareTrait; |
| 32 |
|
| 33 |
const INTERNAL_TYPE = 'internal'; |
| 34 |
const EXTERNAL_TYPE = 'external'; |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
abstract public function get_id(); |
| 40 |
|
| 41 |
/** |
| 42 |
* {@inheritdoc} |
| 43 |
*/ |
| 44 |
abstract public function get_type(); |
| 45 |
|
| 46 |
/** |
| 47 |
* {@inheritdoc} |
| 48 |
*/ |
| 49 |
abstract public function get_label(); |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
*/ |
| 54 |
abstract public function get_description(); |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritdoc} |
| 58 |
*/ |
| 59 |
public function get_licenses() { |
| 60 |
return array( |
| 61 |
'personal', |
| 62 |
'plus', |
| 63 |
'professional', |
| 64 |
'ultimate', |
| 65 |
'elite', |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Determines if the email is active, and can be configured. |
| 71 |
* |
| 72 |
* @since 4.7.3 |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function is_available() { |
| 77 |
$license = simpay_get_license(); |
| 78 |
|
| 79 |
return in_array( $license->get_level(), static::get_licenses(), true ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Determines if the email is enabled, and should send. |
| 84 |
* |
| 85 |
* @since 4.7.3 |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function is_enabled() { |
| 90 |
$enabled = 'yes' === simpay_get_setting( |
| 91 |
sprintf( |
| 92 |
'email_%s', |
| 93 |
static::get_id() |
| 94 |
), |
| 95 |
'yes' |
| 96 |
); |
| 97 |
|
| 98 |
return static::is_available() && $enabled; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns a list of emails that should be forced to use a styled theme. |
| 103 |
* |
| 104 |
* @since 4.7.3 |
| 105 |
* |
| 106 |
* @return array<string> |
| 107 |
*/ |
| 108 |
private function get_style_required_emails() { |
| 109 |
return array( |
| 110 |
'summary-report', |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns the name of the email template to use. |
| 116 |
* |
| 117 |
* @since 4.7.3 |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
private function get_template_name() { |
| 122 |
/** @var string $template */ |
| 123 |
$template = simpay_get_setting( 'email_template', 'none' ); |
| 124 |
|
| 125 |
if ( in_array( static::get_id(), $this->get_style_required_emails(), true ) ) { |
| 126 |
$template = 'default'; |
| 127 |
} |
| 128 |
|
| 129 |
return $template; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* {@inheritdoc} |
| 134 |
*/ |
| 135 |
public function get_template() { |
| 136 |
switch ( $this->get_template_name() ) { |
| 137 |
case 'default': |
| 138 |
return new DefaultTemplate(); |
| 139 |
default: |
| 140 |
return new PlainTemplate(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* {@inheritdoc} |
| 146 |
*/ |
| 147 |
public function get_header_content() { |
| 148 |
$type = $this->get_type(); |
| 149 |
|
| 150 |
switch ( $type ) { |
| 151 |
case 'internal': |
| 152 |
return $this->get_internal_header_content(); |
| 153 |
default: |
| 154 |
return $this->get_external_header_content(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* {@inheritdoc} |
| 160 |
*/ |
| 161 |
public function get_footer_content() { |
| 162 |
$type = $this->get_type(); |
| 163 |
|
| 164 |
switch ( $type ) { |
| 165 |
case 'internal': |
| 166 |
return $this->get_internal_footer_content(); |
| 167 |
default: |
| 168 |
return $this->get_external_footer_content(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns the header content for internal emails. |
| 174 |
* |
| 175 |
* @since 4.7.3 |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
private function get_internal_header_content() { |
| 180 |
$alert = array(); |
| 181 |
|
| 182 |
// If we can use notifications, check for an error notification. |
| 183 |
|
| 184 |
$internal_alerts = $this->notifications->query( |
| 185 |
array( |
| 186 |
'source' => 'internal', |
| 187 |
'type' => 'error', |
| 188 |
'dismissed' => false, |
| 189 |
'is_dismissible' => false, |
| 190 |
) |
| 191 |
); |
| 192 |
|
| 193 |
if ( ! empty( $internal_alerts ) ) { |
| 194 |
/** @var \SimplePay\Core\NotificationInbox\Notification */ |
| 195 |
$notification = $internal_alerts[0]; |
| 196 |
|
| 197 |
$links = array_filter( |
| 198 |
$notification->actions, |
| 199 |
function ( $action ) { |
| 200 |
return 'primary' === $action['type']; |
| 201 |
} |
| 202 |
); |
| 203 |
|
| 204 |
$alert = array( |
| 205 |
'title' => $notification->title, |
| 206 |
'content' => $notification->content, |
| 207 |
'links' => $links, |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
$logo_url = SIMPLE_PAY_URL . 'includes/core/assets/images/wp-simple-pay.png'; |
| 212 |
$image = sprintf( '<img src="%1$s" alt="WP Simple Pay" />', $logo_url ); |
| 213 |
|
| 214 |
ob_start(); |
| 215 |
|
| 216 |
require SIMPLE_PAY_DIR . 'views/email-internal-header-content.php'; |
| 217 |
|
| 218 |
/** @var string $html */ |
| 219 |
$html = ob_get_clean(); |
| 220 |
|
| 221 |
return $html; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Returns the header content for external emails. |
| 226 |
* |
| 227 |
* @since 4.7.3 |
| 228 |
* |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
public function get_external_header_content() { |
| 232 |
/** @var string $image_url */ |
| 233 |
$image_url = simpay_get_setting( 'email_header_image_url', '' ); |
| 234 |
$image = ''; |
| 235 |
|
| 236 |
if ( ! empty( $image_url ) ) { |
| 237 |
$image = sprintf( |
| 238 |
'<a href="%1$s" target="_blank"><img src="%2$s" alt="%3$s" style="max-width: 300px;" /></a>', |
| 239 |
esc_url( home_url() ), |
| 240 |
esc_url( $image_url ), |
| 241 |
get_bloginfo( 'name' ) |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
ob_start(); |
| 246 |
|
| 247 |
require SIMPLE_PAY_DIR . 'views/email-external-header-content.php'; |
| 248 |
|
| 249 |
/** @var string $html */ |
| 250 |
$html = ob_get_clean(); |
| 251 |
|
| 252 |
return $html; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Returns the footer content to load inside the template footer. |
| 257 |
* |
| 258 |
* @since 4.7.3 |
| 259 |
* |
| 260 |
* @return string |
| 261 |
*/ |
| 262 |
private function get_internal_footer_content() { |
| 263 |
$settings_url = Settings\get_url( |
| 264 |
array( |
| 265 |
'section' => 'emails', |
| 266 |
) |
| 267 |
); |
| 268 |
|
| 269 |
$credit = wp_kses( |
| 270 |
sprintf( |
| 271 |
/* translators: %1$s Website name. %2$s Opening anchor tag, do not translate. %3$s Closing anchor tag, do not translate. */ |
| 272 |
__( |
| 273 |
'This email was automatically generated and sent from your website %1$s.<br />Manage %2$semail settings%3$s.', |
| 274 |
'stripe' |
| 275 |
), |
| 276 |
'<a href="' . esc_url( home_url() ) . '">' . get_bloginfo( 'name' ) . '</a>', |
| 277 |
'<a href="' . esc_url( $settings_url ) . '">', |
| 278 |
'</a>' |
| 279 |
), |
| 280 |
array( |
| 281 |
'a' => array( |
| 282 |
'href' => array(), |
| 283 |
), |
| 284 |
'br' => array(), |
| 285 |
) |
| 286 |
); |
| 287 |
|
| 288 |
// @todo Add support for dynamic "Did you know?" blurbs. This should also |
| 289 |
// replace the "Tips" displayed in "Activity & Reports" |
| 290 |
$blurb = array(); |
| 291 |
|
| 292 |
ob_start(); |
| 293 |
|
| 294 |
require SIMPLE_PAY_DIR . 'views/email-internal-footer-content.php'; |
| 295 |
|
| 296 |
/** @var string $html */ |
| 297 |
$html = ob_get_clean(); |
| 298 |
|
| 299 |
return $html; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Returns the footer content for external emails. |
| 304 |
* |
| 305 |
* @since 4.7.3 |
| 306 |
* |
| 307 |
* @return string |
| 308 |
*/ |
| 309 |
public function get_external_footer_content() { |
| 310 |
$content = simpay_get_setting( 'email_footer_content', '' ); |
| 311 |
|
| 312 |
ob_start(); |
| 313 |
|
| 314 |
require SIMPLE_PAY_DIR . 'views/email-external-footer-content.php'; |
| 315 |
|
| 316 |
/** @var string $html */ |
| 317 |
$html = ob_get_clean(); |
| 318 |
|
| 319 |
return $html; |
| 320 |
} |
| 321 |
} |
| 322 |
|