| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormBuilder\EmailPreview\Actions; |
| 4 |
|
| 5 |
/** |
| 6 |
* Build email preview. |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class BuildEmailPreview |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var ApplyPreviewTemplateTags |
| 14 |
*/ |
| 15 |
protected $applyPreviewTemplateTagsAction; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param ApplyPreviewTemplateTags $applyPreviewTemplateTagsAction |
| 19 |
*/ |
| 20 |
public function __construct(ApplyPreviewTemplateTags $applyPreviewTemplateTagsAction) |
| 21 |
{ |
| 22 |
$this->applyPreviewTemplateTagsAction = $applyPreviewTemplateTagsAction; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param string $emailHeader |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function __invoke($request) |
| 30 |
{ |
| 31 |
$formId = $request->get_param('form_id'); |
| 32 |
$emailType = $request->get_param('email_type'); |
| 33 |
|
| 34 |
/** |
| 35 |
* The $emailNotification object is maintained for filter backward compatibility. |
| 36 |
*/ |
| 37 |
try { |
| 38 |
/** @var \Give_Email_Notification $emailNotification */ |
| 39 |
$emailNotification = give(GetEmailNotificationByType::class)->__invoke($emailType); |
| 40 |
} catch (\Exception $e) { |
| 41 |
return new \WP_REST_Response($e->getMessage(), 400); |
| 42 |
} |
| 43 |
|
| 44 |
$emailHeader = apply_filters("give_{$emailType}_get_email_header", $request->get_param('email_heading'), $emailNotification, $formId); |
| 45 |
$emailMessage = apply_filters("give_{$emailType}_get_email_message", $request->get_param('email_message'), $emailNotification, $formId); |
| 46 |
$emailTemplate = apply_filters("give_{$emailType}_get_email_template", $request->get_param('email_template'), $emailNotification, $formId); |
| 47 |
$contentType = apply_filters("give_{$emailType}_get_email_content_type", $request->get_param('email_content_type'), $emailNotification, $formId); |
| 48 |
|
| 49 |
Give()->emails->__set('html', 'text/html' === $contentType); |
| 50 |
Give()->emails->__set('content_type', $contentType); |
| 51 |
Give()->emails->__set('heading', $this->applyPreviewTemplateTags($emailHeader)); |
| 52 |
Give()->emails->__set('template', 'text/html' === $contentType ? $emailTemplate : 'none'); |
| 53 |
|
| 54 |
if('text/plain' === $contentType) { |
| 55 |
$emailMessage = wpautop($emailMessage); |
| 56 |
} |
| 57 |
|
| 58 |
add_filter('give_preview_email_receipt_header', '__return_false'); // Disable hard-coded preview switcher. |
| 59 |
do_action( "give_{$emailType}_email_preview", $emailNotification ); |
| 60 |
|
| 61 |
return apply_filters( "give_{$emailType}_email_preview_message", |
| 62 |
Give()->emails->build_email($this->applyPreviewTemplateTags($emailMessage)), |
| 63 |
$email_preview_data = apply_filters( "give_{$emailType}_email_preview_data", array() ), |
| 64 |
$emailNotification |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @param $emailHeader |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
protected function applyPreviewTemplateTags($emailHeader): string |
| 74 |
{ |
| 75 |
return $this->applyPreviewTemplateTagsAction->__invoke($emailHeader); |
| 76 |
} |
| 77 |
} |
| 78 |
|